Share via


System.Xml.XMLDocument and System.Xml.Linq.XDocument Comparison

This article provides a comparison between System.Xml.XMLDocument and System.Xml.Linq.XDocument. My main purpose of writing this wiki was to explore the System.Xml.Linq library as until recently I mostly used the System.Xml library.

I decided to implement the following scenario for both libraries:

  1. Retrieve an xml feed from the blogs.msdn.com RSS service.
  2. Save the xml feed to a file
  3. Update the in memory xml document by inserting a couple of items
  4. Retrieve the xml feed from the file

The two main criteria I wanted to measure was overall performance of the classes involved and programming convenience.  By programming convenience, I mostly mean was the scenario easy to code and how maintainable (e.g., readable).

Retrieve an xml feed from the blogs.msdn.com RSS service.

System.Xml.Linq.XDocument did make this step incredibly easy:

XDocument xDoc = XDocument.Load("http://blogs.msdn.com/b/mainfeed.aspx?Type=BlogsOnly");

System.Xml.XmlDocument  took a bit more effort in first creating a WebRequest to stream the content into the document's Load() method:

System.Net.WebRequest wRequest = System.Net.WebRequest.Create("http://blogs.msdn.com/b/mainfeed.aspx?Type=BlogsOnly");
System.Net.WebResponse wResponse = wRequest.GetResponse();
System.IO.Stream rssStream = wResponse.GetResponseStream();
 
XmlDocument xDoc = new  XmlDocument();
xDoc.Load(rssStream);

Save the xml feed to file

Both libraries provide a single Save() method for saving to file.  For example,

xDoc.Save("RetrieveFeedXDocumentTests.xml");

Update the in memory xml document by inserting a couple of items

From a programming convenience point of view, System.Xml.Linq.XDocument provides a succinct and rich set of methods.  For example, compare the following:

xDoc.Descendants("item").Last()
                        .AddAfterSelf(new XElement("item",                                                         
                                        new XElement("title", "title1"),
                                        new XElement("link", "link1"),
                                        new XElement("guid",                                                             
                                            new XAttribute("isPermaLink", "false"),
                                            new XText(Guid.NewGuid().ToString()))),
                                        new XElement("item",
                                            new XElement("title", "title2"),
                                            new XElement("link", "link2"),
                                            new XElement("guid",
                                                new XAttribute("isPermaLink", "false"),
                                                new XText(Guid.NewGuid().ToString()))));

with the equivalent System.Xml.XMLDocument:

var xElementItem = xDoc.CreateElement("item");
 
// title
var xTempElement = xDoc.CreateElement("title");
xTempElement.InnerText = "title1";
xElementItem.AppendChild(xTempElement);
 
// link
xTempElement = xDoc.CreateElement("link");
xTempElement.InnerText = "link1";
xElementItem.AppendChild(xTempElement);
 
// guid
xTempElement = xDoc.CreateElement("guid");
var xAttribute = xDoc.CreateAttribute("isPermaLink");
xAttribute.Value = "false";
 
xTempElement.AppendChild(xDoc.CreateTextNode(Guid.NewGuid().ToString()));
xTempElement.Attributes.Append(xAttribute);
             
xElementItem.AppendChild(xTempElement);
 
xDoc.SelectSingleNode("./rss/channel").AppendChild(xElementItem);
 
// second item
xElementItem = xDoc.CreateElement("item");
 
// title
xTempElement = xDoc.CreateElement("title");
xTempElement.InnerText = "title2";
xElementItem.AppendChild(xTempElement);
 
// link
xTempElement = xDoc.CreateElement("link");
xTempElement.InnerText = "link2";
xElementItem.AppendChild(xTempElement);
 
// guid
xTempElement = xDoc.CreateElement("guid");
xAttribute = xDoc.CreateAttribute("isPermaLink");
xAttribute.Value = "false";
 
xTempElement.AppendChild(xDoc.CreateTextNode(Guid.NewGuid().ToString()));
xTempElement.Attributes.Append(xAttribute);
 
xElementItem.AppendChild(xTempElement);
 
xDoc.SelectSingleNode("./rss/channel").LastChild.AppendChild(xElementItem);

Note: I do recognize that I could create methods to assist in building the document.

Retrieve the xml feed from file

Both System.Xml.Linq.XDocument and System.Xml.XmlDocument provide a single method for loading an xml document from a file.  The XDocument example is:

xDoc = XDocument.Load("RetrieveFeedXDocumentTests.xml");

While XmlDocument is:

xDoc.Load("RetrieveFeedXMLDocumentTests.xml");

Performance

Based on other articles I have read about the two technologies, I was expecting a more significant difference in performance where the Linq xml library showed better performance.  I did not though, and found a very slight performance difference in favor of the older System.Xml library:

Linq XDocument Average: 4191
XmlDocument Average: 3829

Please see the MSDN project for full source code: http://code.msdn.microsoft.com/SystemXmlXMLDocument-and-1e664e58