views:

694

answers:

2

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

  1. Deserialize to a strongly typed object and use object properties to access values
  2. use XPath to access values

which approach is faster? some statistics to support your answer would be great.

+1  A: 

There's a third option of sticking with XML, but query with whatever XML API you're using - e.g. LINQ to XML makes queries relatively straightforward in code.

Have you already parsed the text into an XML document?

Are you convinced that this is actually a significant performance bottleneck in your code? (e.g. if you're then talking to a database, then don't worry about this to start with - just get it to work in the simplest way first)

Are the queries always the same, or are they dynamic in some way?

Do you have a test rig with realistic messages and queries? If not, you need one in order to evaluate any answers given here with your data. If you do, I would expect it to be reasonably easy to try it yourself :)

Jon Skeet
Thanks Jon, > Have you already parsed the text into an XML document?yes, I'm receiving an XMLDocument as input> Are the queries always the same, or are they dynamic in some way?queries are always same.I don't have actual data to test with, a rough idea will suffice for now
usman shaheen
I suggest you write it in the easiest way for now (probably XPath) and measure the time taken for the query vs the performance of the whole request. If that proportion ever becomes significant, revisit the issue.
Jon Skeet
+1  A: 

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.

Guge