XML – So many API’s, so many Classes

Whenever I need to process a XML document (every few month), I have to look up XML API details in MSDN, I don’t remember which object to use and which API to use. So here is a cheat sheet. Some outrageous statements are made to help memorize.

XMLReader
To process huge files. Document read as a stream, with a forward only cursor. Does not store or load the whole document, so you can use it to process the document which may not fit into memory. To use it use XmlReader.Create() factory method. Just read, read … read like a fu#$ crazy reader. Hence reader in name.

XPathDocumentRead only. Optimize for querying XMLDocuments. It caches the whole document. Better query performance and smaller memory footprint. If you have path, you can use this for a quick query. Hence the path in name, I guess.

XmlDocment

Read write. Caches the whole document. If you just want to write than use XMLWriter. But for read write you have to use both XMLWriter and XMLReader

  • Prefer LINQ to read XML Document over XmlDocument and XML API’s. Xpath and XDocument wins over LINQ when you need to query the XMLDocument dynamically. Not easy to generates LINQ query on the fly.
  • To write XML document, use XmlWriter. It is the lowest level and fastest API.
  • Avoid // decendant-or-self axis. Prefer to use the full xPath.
  • Avoid absolute xpath and prefer relative xpaths.
  • Avoid using XmlTextWriter and Reader and prefer XmlWriter and XmlReader.
Advertisements

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