A .NET Technology blog | .NET,C# Progamming,C# tutorials,WPF WCF|Software Engineer


XML document parsing in .NET - Best and efiicient options

.Net framework has got various mechanisms for reading and querying XML documents.But what are the pros and cons of each of these methods? which is the best efficient mechanism out of all these? Which is most efficient in terms of performance? Which is simpler and faster to code?

 .Net framework has introduced various mechanisms for reading and querying XMLdocuments . Following are the most frequently used methodologies

  • XmlTextReader
  • XmlDocument (DOM)
  • LINQ to XML and XDocument
  • XpathNavigator
  • XML serializers

Given that, taking a decision of choosing the right method need enough consideration.There are lots of questions such as what are the pros and cons of each of the above methods? which is the best and efficient mechanism out of all these? Which is most efficient in terms of performance? Which is simpler and faster to code?

Even though the decision fully depends up on the type of requirement and many other priority factors the following points will guide you in identifying the best option for your requirement.

XmlTextReader is faster and most memory efficient of all but it can read only in forward direction.  Hence cannot be used for searching the xml document and for navigating back and forth of the aml document.Also XmltextReader is read-only ,so modifying of the document not possible.XmlTextReader is best for usages like reading configuration settings of the an application during start of the application.XmlTextReader does not provide automatic data validation.Developer need to take care of the validation.

XmlDocument reads the entire file in a tree structure that can then be accessed either by browsing through the nodes or by using XPath queries. Memory requirement is huge in XmlDocument since the entire Xml structure gets loded in to memory. So for bigger file XmlDocument is performance and memory overkill. But can be a choice for for smaller files since it is simple to code.

Full traversal (both forward and backward) is possible.Read and write is also possible with XmlDocument .So Search and modify is very much possible with XmlDocument.Performance is too much copmpromised as compared to XmlTextReader and most of the other mechanisms available.Implementation overhead is also more by using XmlDocument.

LINQ to XML  is very easy to code and simple to understand. But by using Linq to Xml mechanism it loads  the whole document into memory to read the xml and hence have high  performance and memory penalty . We can read xml files using Linq to Xml in 2 ways. Using the XDocument class or by the XElement class. Both the classes have a method called Load() which accepts a file, a URL or even a XMLReader and loads the XML.

XpathNavigator is easier than XmlReader to implement but it is lower performance than XmlReader since XmlReade reads one node at a time, but XpathNavigator has to read much more to execute the Xpath query. XmlDocument internally uses XpathNavigator. So evident that the direct usage of XpathNavigator will have performance advantage over XmlDocument.

XML Deserializer and Metamodel DeSerializing the xml document to the corresponding model class will make your life much easier.You create the model class to match the corresponding xml schema and deserialize the entire xml file to the model using the deserialization mechanism. You will get the corresponding metamodel for that xml file.

Pingbacks and trackbacks (1)+

Add comment

  Country flag

  • Comment
  • Preview