views:

557

answers:

2

I'm going on a couple assumptions here:

  1. XPathDocument is not editable.
  2. XmlDocument is editable.
  3. XPathDocument is more efficient for XslCompiledTransform.

That being the case (and please correct me if I am wrong), would it be better (more efficient) to:

  1. Make modifications using the XmlDocument, then convert to an XPathDocument before the transform. Is there an optimized way to do this?
  2. Just stick with the XmlDocument through the transform.

Some background, I'm getting a complex xml from a webservice, then use xpath to find some elements which need to be modified, then use a xslt to create html.

Thanks in advance for the help.

+1  A: 

Just use the XmlDocument class for the transform. It would have to be a massive document to really see a performance gain. You may want to compile a static instance of the XslCompiledTransform class if you plan on using it multiple times.

ChaosPandion
I'm thinking about doing that, I also have already implemented caching of xslt, so I'm not sure how much value using complied versions would add.
aepheus
+1  A: 

It depends on how much modification is required, how big the data is, and whether you are familiar with xslt. If "not" to the last, just use XDocument etc (last time I ran a profile, XDocument was quicker than XmlDocument for my data - run your own tests). Use XDocument.Load / XDocument.Parse, find your node, edit it, and save it.


If you must use xslt, then XDocument isn't really an option; once you've loaded it into XmlDocument you might as well stay with XmlDocument - I would not recommend parsing it into a second DOM.

Of course, it makes we wonder: why modify the xml outside of xslt if you are going to use xslt; perhaps just translate during the xslt?

To get a 100% accurate answer for your data you'll need to profile it, of course. But things such as appropriate use of xslt grouping constructs will often make a much bigger difference than XmlDocument vs XPathDocument.

Marc Gravell
One reason I need to edit the document is to convert date information (need to convert from filetime to something readable). This cannot be done easily without using script in the xslt, which because of a bug in the XslCompiledTransform is off limits. XslCompiledTransform causes files to be written to windows/temp directory when script is used (without any way of setting a different temp directory). Not good in a farm environment, and systems folks will not allow it.Anyhow, using xslt to modify the document is not a good solution (there are lots of dates to change and it's alot easier in c#)
aepheus