XmlDocument: A Simple Example

The XmlDocument class contains everything we need to build an XML document from scratch. The syntax can be tricky if you haven’t used the class before, so this post will hopefully serve as a quick how-to guide.

We’re going to build the following XML document (that follows the XML Sitemap schema):

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://www.mysite.com/link1.aspx</loc>
    <lastmod>2010-06-27</lastmod>
  </url>
  <url>
    <loc>http://www.mysite.com/link2.aspx</loc>
    <lastmod>2010-06-28</lastmod>
  </url>
</urlset>

Let’s start with the first line, the XML declaration:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null));

Notice the version and encoding attributes are added for us in one neat statement. Next we need to create the urlset element and its attribute:

XmlElement urlSet = xmlDoc.CreateElement("urlset");
XmlAttribute xmlns = xmlDoc.CreateAttribute("xmlns");
xmlns.Value = "http://www.sitemaps.org/schemas/sitemap/0.9";

Our attribute needs to be associated with an element, and our element associated with a parent element. Since the urlset is the root element, we can add it to the document by calling the document’s AppendChild() method. Attributes are added by calling the elements Attributes.Append() method:

urlSet.Attributes.Append(xmlns);
xmlDoc.AppendChild(urlSet);

We can make life easier for ourselves if we represent each link as an object:

public class SiteMapLink
{
  public string Location { get; set; }
  public DateTime LastModified { get; set; }
}

Assuming we have a collection of SiteMapLink objects available, we can iterate through them creating the url elements as we go:

// Append site map urls
foreach (SiteMapLink link in urls)
{
  XmlElement url = xmlDoc.CreateElement("url");
  XmlElement loc = xmlDoc.CreateElement("loc");
  XmlElement lastmod = xmlDoc.CreateElement("lastmod");
  XmlText locText = xmlDoc.CreateTextNode(link.Location);
  XmlText lastmodText = xmlDoc.CreateTextNode(link.LastModified.ToString("yyyy-MM-dd"));
  loc.AppendChild(locText);
  lastmod.AppendChild(lastmodText);
  url.AppendChild(loc);
  url.AppendChild(lastmod);
  urlSet.AppendChild(url);
}

To save our XML file, we just need to pass a file name to the XmlDocument’s Save() method:

xmlDoc.Save("sitemap.xml");

The complete code listing looks like this:

public class SiteMap
{
  public static XmlDocument BuildSiteMap(SiteMapLink[] urls)
  {
    // Build site map header
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null));
    XmlElement urlSet = xmlDoc.CreateElement("urlset");
    XmlAttribute xmlns = xmlDoc.CreateAttribute("xmlns");
    xmlns.Value = "http://www.sitemaps.org/schemas/sitemap/0.9";
    urlSet.Attributes.Append(xmlns);
    xmlDoc.AppendChild(urlSet);

    // Append site map urls
    foreach (SiteMapLink link in urls)
    {
      XmlElement url = xmlDoc.CreateElement("url");
      XmlElement loc = xmlDoc.CreateElement("loc");
      XmlElement lastmod = xmlDoc.CreateElement("lastmod");
      XmlText locText = xmlDoc.CreateTextNode(link.Location);
      XmlText lastmodText = xmlDoc.CreateTextNode(link.LastModified.ToString("yyyy-MM-dd"));
      loc.AppendChild(locText);
      lastmod.AppendChild(lastmodText);
      url.AppendChild(loc);
      url.AppendChild(lastmod);
      urlSet.AppendChild(url);
    }
    return xmlDoc;
  }

  public class SiteMapLink
  {
    public string Location { get; set; }
    public DateTime LastModified { get; set; }
  }
}
Advertisements
This entry was posted in Reference and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s