locked
Create a sitemap for my website with asp.net. How add text to xml? RRS feed

  • Question

  • User-909867351 posted

    Hi

    I want to create a sitemap.xml to my website following the strandard especified at http://www.sitemaps.org/protocol.html

    I can create the XML file with the following code:

     XDocument doc = new XDocument(               
                    new XElement("url",new XElement("loc", "http://linka.com"),            
                    new XElement("lastmod", "2016-04-24"),
                    new XElement("changefreq", "weekly"),        
                    new XElement("priority", "0.8")
                        )
            );      
    doc.Save("C:\\produtosvendanet\\sitemap\\sitemap.xml");

    And I get the a correct xml file . My problem is the sentence

    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    

    How can I insert this  (and

    </urlset> 

     ) on my XML file?

    Thank you

    Sunday, April 24, 2016 4:25 PM

Answers

  • User-158764254 posted

    I think maybe something like this:

    XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
    XElement urlset = new XElement(ns + "urlset");
    urlset.Add(
        new XElement(ns + "url", 
            new XElement(ns + "loc", "http://linka.com"),
            new XElement(ns + "lastmod", "2016-04-24"),
            new XElement(ns + "changefreq", "weekly"),
            new XElement(ns + "priority", "0.8")
         )
    );
    XDocument doc = new XDocument( urlset );
    doc.Save("C:\\produtosvendanet\\sitemap\\sitemap.xml");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 24, 2016 6:35 PM

All replies

  • User-1780421697 posted

    Sample:

    <?xml version="1.0" encoding="UTF-8"?>
    
    -<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
    
    -<url>
    
    <loc>http://thecars.ae/</loc>
    
    <changefreq>weekly</changefreq>
    
    </url>
    
    
    -<url>
    
    <loc>http://thecars.ae/home</loc>
    
    <changefreq>weekly</changefreq>
    
    </url>
    
    
    -<url>
    
    <loc>http://thecars.ae/Account/Login?ReturnUrl=%2FDealer%2FDashboard</loc>
    
    <changefreq>weekly</changefreq>
    
    </url>
    
    
    -<url>
    
    <loc>http://thecars.ae/used-cars/search</loc>
    
    <changefreq>weekly</changefreq>
    
    </url>
    
    
    -<url>
    
    <loc>http://thecars.ae/Account/Login?ReturnUrl=%2FUsedCarsUser%2FDashboard%2FAdvertisements</loc>
    
    <changefreq>weekly</changefreq>
    
    </url>
    
    
    -<url>
    
    <loc>http://thecars.ae/Account/Login?ReturnUrl=%2FUsedCarsDealer%2FDashboard</loc>
    
    <changefreq>weekly</changefreq>
    
    </url>
    
    
    -<url>
    
    <loc>http://thecars.ae/Account/Login?ReturnUrl=%2FUsedCarsUser%2FDashboard%2FAdvertisementPosting</loc>
    
    <changefreq>weekly</changefreq>
    
    </url>
    
    
    -<url>
    
    <loc>http://thecars.ae/Account/Register</loc>
    
    <changefreq>weekly</changefreq>
    
    </url>
    
    
    -<url>
    
    <loc>http://thecars.ae/Account</loc>
    
    <changefreq>weekly</changefreq>
    
    </url>
    
    
    -<url>
    
    <loc>http://thecars.ae/Account/ForgotPassword</loc>
    
    <changefreq>weekly</changefreq>
    
    </url>
    
    </urlset>

    Sunday, April 24, 2016 4:45 PM
  • User-158764254 posted

    I think maybe something like this:

    XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
    XElement urlset = new XElement(ns + "urlset");
    urlset.Add(
        new XElement(ns + "url", 
            new XElement(ns + "loc", "http://linka.com"),
            new XElement(ns + "lastmod", "2016-04-24"),
            new XElement(ns + "changefreq", "weekly"),
            new XElement(ns + "priority", "0.8")
         )
    );
    XDocument doc = new XDocument( urlset );
    doc.Save("C:\\produtosvendanet\\sitemap\\sitemap.xml");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 24, 2016 6:35 PM