I'm going on a couple assumptions here:
- XPathDocument is not editable.
- XmlDocument is editable.
- 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:
- Make modifications using the XmlDocument, then convert to an XPathDocument before the transform. Is there an optimized way to do this?
- 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.
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
.
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.
精彩评论