Kumar
Kumar

Reputation: 11349

What is the most performant code for xml string to objects

I'm trying to optimize some existing code ( where speed is the primary criteria ) which loads an xml string into XmlDocument and then gets the values like so

XmlNodeList listaa = xmlDoc.GetElementsByTagName("aa");
if (listaa.Count > 0)
                myObj.aa = Convert.ToDouble(listaa[0].InnerText);

and so on

while the above is obviously not the best approach, wondering if there's anything new in .net 4.0 that might improve the speed and perhaps readability as well

thanks

Note - the xml strings are approx 100-200 bytes in size so there's not really a whole lot of performance improvement using xmlreader vs. reading to a xmldocument !

Upvotes: 1

Views: 98

Answers (2)

Thomas Levesque
Thomas Levesque

Reputation: 292425

You can use XML serialization. It's quite efficient because it generates serialization assemblies and reuses them (you can pre-generate the assemblies for better performance on the first run)

Upvotes: 1

Pashec
Pashec

Reputation: 23289

The fastest approach is to use old and famous XmlReader. Though readability of your code will be worse but you will get the highest performance.

Upvotes: 3

Related Questions