需要编辑时使用 XmlDocument 或 XPathDocument 的 C# XslCompiledTransform?
我在这里进行几个假设:
- XPathDocument 不可编辑。
- XmlDocument 是可编辑的。
- XPathDocument 更有效 XslCompiledTransform。
既然如此(如果我错了,请纠正我),是否会更好(更有效):
- 使用 XmlDocument,然后转换为 转换之前的 XPathDocument。 有没有优化的方法 这?
- 只需坚持使用 XmlDocument 通过变换。
一些背景知识,我从 Web 服务获取复杂的 xml,然后使用 xpath 查找一些需要修改的元素,然后使用 xslt 创建 html。
预先感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
取决于需要修改多少,数据有多大,以及你是否熟悉xslt。如果最后“不是”,只需使用
XDocument
等(上次我运行配置文件时,XDocument
比XmlDocument
我的数据 - 运行您自己的测试)。使用XDocument.Load
/XDocument.Parse
,找到您的节点,对其进行编辑并保存。如果您必须使用 xslt,那么
XDocument
并不是一个真正的选择;一旦您将其加载到XmlDocument
中,您不妨停留使用XmlDocument
- 我不建议将其解析为秒< /em> DOM。当然,这让我们想知道:如果要使用xslt,为什么要修改xslt外部的xml?也许只是在 xslt 期间翻译?
当然,要为您的数据获得 100% 准确的答案,您需要对其进行分析。但是,诸如适当使用 xslt 分组构造之类的事情通常会比
XmlDocument
与XPathDocument
产生更大的差异。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 thanXmlDocument
for my data - run your own tests). UseXDocument.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 intoXmlDocument
you might as well stay withXmlDocument
- 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
vsXPathDocument
.只需使用
XmlDocument
类进行转换即可。要想真正看到性能提升,就必须有一个庞大的文档。如果您计划多次使用 XslCompiledTransform 类,您可能需要编译该类的静态实例。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 theXslCompiledTransform
class if you plan on using it multiple times.