コード例 #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);
        }