I’m passing small (2-10 KB)XML documents as input to a WCF service. now I’ve two option to read data values from incoming XML
- Deserialize to a strongly typed object and use object properties to access values
- use XPath to access values
which approach is faster? some statistics to support your answer would be great.
I would deserialize it.
If you use xpath, you will deserialize (or ‘load’) it to XmlDocument or something anyway. So both solutions use time deserializing. After this is done, xpath will be slower because of the time spent parsing that string, resolving names, executing functions and so on. Also, if you go with xpath, you get no type safety. Your compiler can’t check the xpath syntax for you.
If you use XmlSerializer and classes, you get static typing. Really fast access to you data, and if you want to query them with xpath, there are still ways to do that.
Also, I would like to say that your code would probably be easier to understand with classes.
The only drawback is that the xml has to conform to the same schema all the time, but that might not be a real problem in your case.
I hope that you forgive the absence of statistics, I think the arguments are strong enough without examples. If you want an ultimate answer, try both and keep a stopwatch ready.