<?xml version="1.0" encoding="UTF-8"?><item><title>Creating a Google Sitemap for a dynamic site with ASP.NET</title><description>&lt;P&gt;Google's introduced a thing called Sitemaps, where you can describe your site using a simple XML syntax, and they'll pull this and use it as hints to their crawler to influence how and when it crawls your site.&amp;nbsp; They probably could have used RSS for this, but I guess that's a point I'll let &lt;A href="http://www.scripting.com/"&gt;Dave Winer&lt;/A&gt; make (I haven't checked to see if he has, yet, but I'm sure he will).&lt;/P&gt;&#13;
&lt;P&gt;The &lt;A href="http://www.ottawaevents.org/"&gt;Ottawa Events&lt;/A&gt; site is somewhat unique in that events are posted, they're valid until the date that the event occurs, and then there's really no point for a search engine to hang onto them anymore.&amp;nbsp; I don't expire events from the database (yet), so the URLs still work, and Google will still have it indexed, but really, if you're looking for "glebe garage sale" you probably care about the next one coming, not one from 2 years ago.&lt;/P&gt;&#13;
&lt;P&gt;I cooked up a &lt;A href="http://www.ottawaevents.org/sitemap.aspx"&gt;sitemap.aspx&lt;/A&gt; page that generates a sitemap on the fly for the main pages of the site, and the current events.&amp;nbsp; If Google indexes only these, and not other URLs it might find, then the results will be more useful than if they indexed everything they could find.&lt;/P&gt;&#13;
&lt;P&gt;Creating the sitemap was simple:&amp;nbsp; I did the same DB query that I use to generate the front page, and I dump all that into a canned XML document that describes the static pages on the site.&amp;nbsp; I don't ping Google when a new event is submitted, yet, but I'll add that shortly.&lt;/P&gt;&#13;
&lt;P&gt;The code is simple enough:&lt;/P&gt;&#13;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&amp;nbsp; _doc = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;XmlDocument&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;();&lt;BR&gt;&amp;nbsp; _doc.Load(&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;"&lt;CANNED path file xml&gt;"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;);&lt;BR&gt;&amp;nbsp; &lt;/FONT&gt;&lt;FONT size=2&gt;_urlset = _doc.SelectSingleNode(&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;"/urlset"&lt;/FONT&gt;&lt;FONT size=2&gt;);&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&#13;
&lt;P&gt;&lt;FONT size=3&gt;Then do the DB query to find all the rows I want to add entries for.&amp;nbsp; Once I've got &lt;FONT size=3&gt;that&lt;/FONT&gt;:&lt;/FONT&gt;&lt;/P&gt;&lt;FONT size=2&gt;&#13;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp; foreach&lt;/FONT&gt;&lt;FONT size=2&gt; (&lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;DataRow&lt;/FONT&gt;&lt;FONT size=2&gt; row &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;in&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt; ds.Tables[0].Rows)&lt;BR&gt;&amp;nbsp; {&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;BR&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; addUrl(&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#800000 size=2&gt;"EventDetails.aspx?id="&lt;/FONT&gt;&lt;FONT size=2&gt; + row[&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;"ID"&lt;/FONT&gt;&lt;FONT size=2&gt;].ToString(), &lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;"daily"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;);&lt;BR&gt;&amp;nbsp; }&lt;/FONT&gt;&lt;FONT size=3&gt;&lt;/P&gt;&#13;
&lt;P&gt;The addUrl function just creates the &lt;URL&gt;&lt;FONT size=3&gt;node&lt;/FONT&gt;:&lt;/P&gt;&lt;FONT size=2&gt;&#13;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp; void&lt;/FONT&gt;&lt;FONT size=2&gt; addUrl(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;string&lt;/FONT&gt;&lt;FONT size=2&gt; url, &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;string&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt; changeFreq)&lt;BR&gt;&amp;nbsp; {&lt;BR&gt;&amp;nbsp; &amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;XmlNode&lt;/FONT&gt;&lt;FONT size=2&gt; urlNode = _doc.CreateElement(&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;"url"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;);&lt;BR&gt;&amp;nbsp; &amp;nbsp; appendValue(urlNode, &lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;"loc"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;, url);&lt;BR&gt;&amp;nbsp; &amp;nbsp; appendValue(urlNode, &lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;"changefreq"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;, changeFreq);&lt;BR&gt;&amp;nbsp; &amp;nbsp; _urlset.AppendChild(urlNode);&lt;BR&gt;&amp;nbsp; }&lt;/FONT&gt;&lt;/P&gt;&#13;
&lt;P&gt;&lt;FONT size=3&gt;Then at the end of Page_Load, I send the contents of the XML doc to the output stream and end the request:&lt;/FONT&gt;&lt;/P&gt;&lt;FONT size=2&gt;&lt;FONT size=2&gt;&#13;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp; _doc.Save(Response.OutputStream);&lt;BR&gt;&amp;nbsp; Response.End();&lt;/FONT&gt;&lt;/P&gt;&#13;
&lt;P&gt;Google hasn't pulled it yet, so I can't say if it works yet... but it should.. :)&lt;/P&gt;&#13;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;</description><pubDate>Thu, 01 Sep 2005 19:17:09 GMT</pubDate></item>