1

My website has dynamic content, new links are being created.

my db has a table which pretty much contains all the added urls.

So my question is how importnt is it for me to have sitemaps.xml, and also is there a simple way to build it so that when new links are generated i can tag it to the end of a sitemap.xml file?

raklos
  • 28,027
  • 60
  • 183
  • 301

2 Answers2

2

It is important for crawlers to be able to crawl your site quicker and with more accurate.

You can create a controller, say siteMapController and in Index add the following

 public ActionResult Index() {
        var xmlString =
            "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">";

        xmlString +=
                "<url>" +
               "<loc>Your site</loc>" +
               "<changefreq>weekly</changefreq>" +
               "<priority>0.6</priority>" +
             "</url>" +
           "<url>" +
                    "<loc>Static Link of you site</loc>" + //you can add as many you want
                       "<changefreq>weekly</changefreq>" +
                       "<priority>0.6</priority>" +
                    "</url>" +
                    "<url>";


           //Dynamic links
            xmlString += "<url>" +
                         "<loc>Link of new item"</loc>" +
                         "<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>" +
                         "<changefreq>daily</changefreq>" +
                         "<priority>0.8</priority>" +
                         "</url>";
        }
        xmlString += "</urlset>";
        ViewData["siteMap"] = xmlString;
        return View();
    }

save the xml on your server and post the sitemap via this link

https://www.google.com/webmasters/tools/home?hl=en

hope that helps

profanis
  • 2,741
  • 3
  • 39
  • 49
  • What if you have 20,000 dynamically generated pages on your site? Should you include them all in the site map, or will Google disregard such a large entry. Also, you should generally avoid building XML via string concatenation. Either write directly to the response stream or use the XML APIs or a StringBuilder in the worst case. – Drew Noakes Feb 17 '11 at 11:05
  • okay so this will giveme an xml file at http://www.mysite.com/sitemap/ ? the problem is, when newly created links are added to the db will i have to rerender the entire xml file? – raklos Feb 17 '11 at 11:08
2

You can use LINQ to XML to create an XML sitemap from your database, then return the sitemap from an action by returning Content(document.ToString(), "text/xml").

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • the problem is how do i go about newly added urls, because i dont want it to have to loop through all the urls's everytime the crawler hits it. – raklos Feb 17 '11 at 14:27
  • 1
    @raklos: You should loop through the URLs every time. There's nothing wrong with that. – SLaks Feb 17 '11 at 14:30
  • 1
    If you really want to, you can run code that writes the file to disk whenever the database is changed, but I wouldn't recommend it. – SLaks Feb 17 '11 at 14:31
  • if i choose your way, would displaying just the previous weeks links be enough? - as in google will still have records of older links. also instead of linq to xml could i populate the xml within a razor view? – raklos Feb 17 '11 at 16:11
  • 1
    @raklos: No; you should always list all links. There is no point in using Razor here; it would be easier to use LINQ to XML to generate a string. However, you can if you want to. – SLaks Feb 17 '11 at 16:12
  • I have created this, would telling google my sitemap is located at www.mysite.com/sitemap be enough?(ie without the extension .xml) or do i have to save it as a sitemap.xml file and submit it? – raklos Feb 18 '11 at 12:09
  • @rawlos: The extension shouldn't matter at all. If you want to, though, you can add an extension in the route. – SLaks Feb 18 '11 at 12:12
  • What if you have 20,000 dynamically generated pages on your site? Should you include them all in the site map, or will Google disregard such a large entry? – Drew Noakes Feb 18 '11 at 13:11
  • @Drew: Send them all. Google will handle it. StackOverflow is an example of a **huge** sitemap. ​​http://meta.stackexchange.com/questions/19805/does-stackoverflow-have-a-sitemap/19806#19806 – SLaks Feb 18 '11 at 13:12
  • Thanks very much. I'll need to tackle this soon for a site I'm building which has a tonne of pages. – Drew Noakes Feb 18 '11 at 17:51