-1

This question is the same as XML sitemap remove xmlns from url tag but that one was never answered . Right now my xml returns as follow

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url xmlns="">
<loc>https://www.localhost.com/J-B-Lansing-Co-CA/1/Hsuhsus</loc>
</url>
<url xmlns="">
<loc>
https://www.localhost.com/J-B-Lansing-Co-CA/2/Swhuwhsw-wshwshusws
</loc>
</url>
</urlset>

I am focused on this part here <url xmlns=""> I need to remove the xmlns part and have been unsuccessful . My code is as follows

       [Route("sitemap")]
        public async Task<ContentResult> SiteMap()
        {
            var result = await _homeRepository.URLMapper();


            XNamespace nsSitemap = "http://www.sitemaps.org/schemas/sitemap/0.9";
            var urlSet = new XElement(nsSitemap + "urlset",
                result.Select(x =>
                    new XElement("url",
                       new XElement("loc", x.URL))));


            return new ContentResult
            {
                ContentType = "text/xml",
                Content = urlSet.ToString(),
                StatusCode = 200
            };
        }

The result url mapper simply returns a list of urls from the database . Any suggestions would be great .

user1591668
  • 2,591
  • 5
  • 41
  • 84
  • If you want the URLs use : List elements = doc.Descendants().Where(x => x.Name.LocalName == "url").ToList(); – jdweng May 19 '20 at 17:51

1 Answers1

0

You need to preface those XElement names with the namespace: This demonstrates the solution:

XNamespace nsSitemap = "http://www.sitemaps.org/schemas/sitemap/0.9";
            var urlSet = new XElement(
                nsSitemap + "urlset",
                new[] { new { URL = "https://www.localhost.com/J-B-Lansing-Co-CA/1/Hsuhsus" }}
                    .Select(x =>
                        new XElement(nsSitemap + "url",
                           new XElement(nsSitemap + "loc", x.URL)
                        )
                    )
            );
        Console.WriteLine(urlSet.ToString());

Also created a DotNetFiddle

peinearydevelopment
  • 11,042
  • 5
  • 48
  • 76