Beispiel #1
0
        public virtual string GenerateXmlString(params SiteMapItem[] items)
        {
            if (items.Any() != true)
            {
                throw new ArgumentNullException($"{nameof(items)} is empty.");
            }

            var siteMapCount = (int)Math.Ceiling(items.Length / (double)SiteMapValidator.MaximumSiteMapIndexCount);

            SiteMapValidator.CheckSiteMapCount(siteMapCount);

            var siteMapXml =
                new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XElement(Xmlns + "urlset",
                                 new XAttribute("xmlns", Xmlns),
                                 new XAttribute(XNamespace.Xmlns + "xsi", Xsi),
                                 new XAttribute(Xsi + "schemaLocation",
                                                @"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
                                 from item in items
                                 select CreateItemElement(item)
                                 )
                    );

            var xml = siteMapXml.ToString(Encoding.UTF8);

            SiteMapValidator.CheckDocumentSize(xml);

            return(xml);
        }
        private static string CreateItemElement(SiteMapImageItemModel item)
        {
            var itemElement = new XElement(Xmlns + "url", new XElement(Xmlns + "loc", item.Url.ToLowerInvariant()));

            foreach (var siteMapImageDetail in item.Images)
            {
                var imageElement = new XElement(Image + "image");

                // all other elements are optional
                imageElement.Add(new XElement(Image + "loc", siteMapImageDetail.ImagePath.ToLowerInvariant()));

                if (!string.IsNullOrWhiteSpace(siteMapImageDetail.Caption))
                {
                    imageElement.Add(new XElement(Image + "caption", siteMapImageDetail.Caption));
                }

                if (!string.IsNullOrWhiteSpace(siteMapImageDetail.GeoLocation))
                {
                    imageElement.Add(new XElement(Image + "geo_location", siteMapImageDetail.GeoLocation));
                }

                if (!string.IsNullOrWhiteSpace(siteMapImageDetail.Title))
                {
                    imageElement.Add(new XElement(Image + "title", siteMapImageDetail.Title));
                }

                if (!string.IsNullOrWhiteSpace(siteMapImageDetail.License))
                {
                    imageElement.Add(new XElement(Image + "license", siteMapImageDetail.License));
                }

                itemElement.Add(imageElement);
            }

            var document = new XDocument(itemElement);

            var xml = document.ToString(Encoding.UTF8);

            SiteMapValidator.CheckDocumentSize(xml);

            return(xml);
        }
        public virtual string GenerateXmlString(params SiteMapImageItemModel[] items)
        {
            if (items?.Any() != true)
            {
                throw new ArgumentNullException($"{nameof(items)} is null");
            }

            var siteMap = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XElement(Xmlns + "urlset",
                             new XAttribute("xmlns", Xmlns),
                             new XAttribute(XNamespace.Xmlns + "image", Image),
                             from item in items
                             select CreateItemElement(item)
                             )
                );

            var xml = siteMap.ToString(Encoding.UTF8);

            SiteMapValidator.CheckDocumentSize(xml);

            return(xml);
        }
Beispiel #4
0
        public virtual string GenerateXmlString(params SiteMapIndexItemModel[] items)
        {
            if (items?.Any() != true)
            {
                throw new ArgumentNullException($"{nameof(items)} is null");
            }

            var siteMap = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XElement(Xmlns + "sitemapindex",
                             new XAttribute("xmlns", Xmlns),
                             new XAttribute(XNamespace.Xmlns + "xsi", Xsi),
                             new XAttribute(Xsi + "schemaLocation",
                                            @"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd"),
                             from item in items
                             select CreateItemElement(item)
                             )
                );

            var xml = siteMap.ToString(Encoding.UTF8);

            SiteMapValidator.CheckDocumentSize(xml);
            return(xml);
        }