posted on Tuesday, June 22, 2004 8:37 AM by leon

Choose right class for XML processing

How are you processing your XML in .NET?

Having all this reach API for XML processing, selection of the right classes is the key to building well performing, reliable code. Basically you have 3 choices:
1. Using derivatives of XMLReader/XMLWriter
     XmlTextReader xr = new XmlTextReader(new StringReader(xmlData));
     xr.Read();

2. Using XMLDocument (proved, MSXML veteran object)
    XmlDocument xdoc = new XmlDocument();
    xdoc.LoadXml(xmlData);
    XmlNodeList xnl = xdoc.SelectNodes("/Clients");

3. Using optimized, lightweight  XPathDocument (from System.Xml.XPath namespace) in conjunction with XPathNavigator
    XPathDocument xpdoc = new XPathDocument(new StringReader(xmlData));
    XPathNavigator xpn = xpdoc.CreateNavigator();
    XPathException xpe = xpn.Compile("/Clients");
    XPathNodeIterator xpni = xpn.Select(xpe);

Here are mine recommendations:
Use XMLReader/XMLWriter in performance oriented scenarios. Validation could be performed with XMLValidatingReader. You will pay development cost for speed.

Use XPathDocument for everything else!
Reasons:
1. In .NET 2.0 it becomes Read/Write.
2. XPathDocument optimized for XSLT and XPath processing, has less footprint, resulting in much better performance relative to XmlDocument. In some cases it will perform 10 times faster, and becomes even faster in Whidbey.
3. In .NET 2.0 can be used with XQueries.

 

Comments