XDocument 或 XmlDocument
我现在正在学习 XmlDocument
但我刚刚遇到 XDocument< /code>
当我尝试搜索它们的区别或优点时,我找不到有用的东西,您能告诉我为什么您会使用其中一个而不是另一个吗?
I am now learning XmlDocument
but I've just ran into XDocument
and when I try to search the difference or benefits of them I can't find something useful, could you please tell me why you would use one over another ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您使用的是 .NET 3.0 或更低版本,则必须使用
XmlDocument
也称为经典 DOM API。同样,您会发现还有一些其他 API 也需要这样做。不过,如果您可以选择,我强烈建议您使用 XDocument(又名 LINQ to XML)。创建文档并处理它们更加简单。例如,它的区别在于:
命名
空间在 LINQ to XML 中使用起来非常容易,这与我见过的任何其他 XML API 不同:
LINQ to XML 也可以很好地与 LINQ 配合使用 - 它的构造模型允许您构建元素使用子元素序列非常容易:
它更具声明性,符合一般的 LINQ 风格。
现在,正如 Brannon 提到的,这些是内存中 API,而不是流式 API(尽管 XStreamingElement 支持延迟输出)。
XmlReader
和XmlWriter
是在 .NET 中流式处理 XML 的常用方法,但您可以在某种程度上混合使用所有 API。例如,您可以流式传输大型文档,但使用 LINQ to XML,方法是将XmlReader
放置在元素的开头,从中读取XElement
并处理它,然后移动到下一个元素等。有关于此技术的各种博客文章,这是我通过快速搜索找到的一个。If you're using .NET version 3.0 or lower, you have to use
XmlDocument
aka the classic DOM API. Likewise you'll find there are some other APIs which will expect this.If you get the choice, however, I would thoroughly recommend using
XDocument
aka LINQ to XML. It's much simpler to create documents and process them. For example, it's the difference between:and
Namespaces are pretty easy to work with in LINQ to XML, unlike any other XML API I've ever seen:
LINQ to XML also works really well with LINQ - its construction model allows you to build elements with sequences of sub-elements really easily:
It's all a lot more declarative, which fits in with the general LINQ style.
Now as Brannon mentioned, these are in-memory APIs rather than streaming ones (although
XStreamingElement
supports lazy output).XmlReader
andXmlWriter
are the normal ways of streaming XML in .NET, but you can mix all the APIs to some extent. For example, you can stream a large document but use LINQ to XML by positioning anXmlReader
at the start of an element, reading anXElement
from it and processing it, then moving on to the next element etc. There are various blog posts about this technique, here's one I found with a quick search.令我惊讶的是,到目前为止,没有一个答案提到
XmlDocument
不提供行信息,而XDocument
提供 (通过IXmlLineInfo
接口)。在某些情况下,这可能是一个关键功能(例如,如果您想要报告 XML 中的错误,或者跟踪一般定义元素的位置),并且在您愉快地开始使用
实现之前,您最好意识到这一点XmlDocument
,后来发现您必须全部更改。I am surprised none of the answers so far mentions the fact that
XmlDocument
provides no line information, whileXDocument
does (through theIXmlLineInfo
interface).This can be a critical feature in some cases (for example if you want to report errors in an XML, or keep track of where elements are defined in general) and you better be aware of this before you happily start to implement using
XmlDocument
, to later discover you have to change it all.XmlDocument
对于熟悉 XML DOM 对象模型的开发人员来说非常有用。它已经存在了一段时间,或多或少符合 W3C 标准。它支持手动导航以及 XPath 节点选择。XDocument
为 .NET 3.5 中的 LINQ to XML 功能提供支持。它大量使用了IEnumerable
,并且可以更轻松地直接在 C# 中使用。这两种文档模型都要求您将整个文档加载到内存中(例如与
XmlReader
不同)。XmlDocument
is great for developers who are familiar with the XML DOM object model. It's been around for a while, and more or less corresponds to a W3C standard. It supports manual navigation as well asXPath
node selection.XDocument
powers the LINQ to XML feature in .NET 3.5. It makes heavy use ofIEnumerable<>
and can be easier to work with in straight C#.Both document models require you to load the entire document into memory (unlike
XmlReader
for example).正如其他地方所提到的,毫无疑问,与
XmlDocument
相比,Linq to Xml 使 xml 文档的创建和更改变得轻而易举,并且XNamespace ns + "elementName"
语法使阅读变得愉快当处理命名空间时。对于
xsl
和xpath
顽固分子来说,值得一提的是,仍然可以在 Linq 2 Xml 上执行任意xpath 1.0
表达式< code>XNodes 通过包含:然后我们可以通过这些扩展方法使用
xpath
导航和投影数据:例如,给定 Xml 文档:
我们可以评估:
As mentioned elsewhere, undoubtedly, Linq to Xml makes creation and alteration of xml documents a breeze in comparison to
XmlDocument
, and theXNamespace ns + "elementName"
syntax makes for pleasurable reading when dealing with namespaces.One thing worth mentioning for
xsl
andxpath
die hards to note is that it IS possible to still execute arbitraryxpath 1.0
expressions on Linq 2 XmlXNodes
by including:and then we can navigate and project data using
xpath
via these extension methods:For instance, given the Xml document:
We can evaluate:
XDocument
来自 LINQ to XML API,而XmlDocument
是用于 XML 的标准 DOM 样式 API。如果您很了解 DOM,并且不想学习 LINQ to XML,请使用XmlDocument
。如果您对两者都不熟悉,请查看此页面,其中比较了两个,然后选择你更喜欢哪一个的外观。我刚刚开始使用 LINQ to XML,并且我喜欢使用函数式构造创建 XML 文档的方式。真的很好。相比之下,DOM 就显得笨拙了。
XDocument
is from the LINQ to XML API, andXmlDocument
is the standard DOM-style API for XML. If you know DOM well, and don't want to learn LINQ to XML, go withXmlDocument
. If you're new to both, check out this page that compares the two, and pick which one you like the looks of better.I've just started using LINQ to XML, and I love the way you create an XML document using functional construction. It's really nice. DOM is clunky in comparison.
另请注意,Xbox 360 和 Windows Phone OS 7.0 支持
XDocument
。如果您的目标是它们,请针对
XDocument
进行开发或从XmlDocument
迁移。Also, note that
XDocument
is supported in Xbox 360 and Windows Phone OS 7.0.If you target them, develop for
XDocument
or migrate fromXmlDocument
.我相信
XDocument
进行了更多的对象创建调用。我怀疑当您处理大量 XML 文档时,XMLDocument
会更快。发生这种情况的一个地方是管理扫描数据。许多扫描工具以 XML 格式输出数据(出于显而易见的原因)。如果您必须处理大量此类扫描文件,我认为使用
XMLDocument
可以获得更好的性能。I believe that
XDocument
makes a lot more object creation calls. I suspect that for when you're handling a lot of XML documents,XMLDocument
will be faster.One place this happens is in managing scan data. Many scan tools output their data in XML (for obvious reasons). If you have to process a lot of these scan files, I think you'll have better performance with
XMLDocument
.