locked
sitemap error help to remove extra tag parameter RRS feed

  • Question

  • User-1327394822 posted

    Hi,

    I am using the below lines to generate sitemap but google says there is an error. I know the error but i am unable to figure out how to remove the tag.

    THe code

    @using System.Xml.Linq;
    @{
        Layout = null;
        var urls = new List<string>{
          "1", "2", "3"
        };
        XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
        var baseurl = "http://www.myurl.in/{0}";
        var sitemap = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
                new XElement(ns + "urlset", 
                    from url in urls select
                    new XElement("url",
                        new XElement("loc", string.Format(baseurl, url)),
                        new XElement("lastmod", string.Format("{0}{1}", DateTime.Now.ToString("s"), DateTime.Now.ToString("%K"))),
                        new XElement("changefreq", "monthly"),
                        new XElement("priority", "0.5")
                    )
                )  
        );
        Response.ContentType = "text/xml";
        sitemap.Save(Response.Output);
    }

    Output is 

    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        <url xmlns="">
            <loc>http://www.myurl.in/1</loc>
            <lastmod>2016-06-17T15:19:27+05:30</lastmod>
            <changefreq>daily</changefreq>
        </url>
        <url xmlns="">
            <loc>http://www.myurl.in/2</loc>
            <lastmod>2016-06-17T15:19:27+05:30</lastmod>
            <changefreq>daily</changefreq>
        </url>
        <url xmlns="">
            <loc>http://www.myurl.in/3</loc>
            <lastmod>2016-06-17T15:19:27+05:30</lastmod>
            <changefreq>daily</changefreq>
        </url>
    </urlset>

    i want to remove the xmlns="" from the url tag.

    <url xmlns="">

    should be

    <url>



    Friday, June 17, 2016 10:06 AM

Answers

  • User-821857111 posted
    from url in urls select
    new XElement(ns + "url",
        new XElement(ns + "loc", string.Format(baseurl, url)),
        new XElement(ns + "lastmod", string.Format("{0}{1}", DateTime.Now.ToString("s"), DateTime.Now.ToString("%K"))),
        new XElement(ns + "changefreq", "monthly"),
        new XElement(ns + "priority", "0.5")
    )
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 17, 2016 12:23 PM

All replies

  • User-821857111 posted
    from url in urls select
    new XElement(ns + "url",
        new XElement(ns + "loc", string.Format(baseurl, url)),
        new XElement(ns + "lastmod", string.Format("{0}{1}", DateTime.Now.ToString("s"), DateTime.Now.ToString("%K"))),
        new XElement(ns + "changefreq", "monthly"),
        new XElement(ns + "priority", "0.5")
    )
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 17, 2016 12:23 PM
  • User-1327394822 posted

    Thank you Mike!

    Saturday, June 18, 2016 8:55 AM