简体   繁体   English

XElement和XDocument之间有什么区别?

[英]What's the difference between XElement and XDocument?

XElementXDocument什么区别,你什么时候使用它们?

XDocument represents a whole XML document. XDocument表示整个XML文档。 It is normally composed of a number of elements. 它通常由许多元素组成。

XElement represents an XML element (with attributes, children etc). XElement表示XML元素(具有属性,子元素等)。 It is part of a larger document. 它是更大文档的一部分。

Use XDocument when working with a whole XML document, XElement when working with an XML element. 在处理XML元素时使用整个XML文档XElement时使用XDocument。

For example - XElement has a HasAttributes property indicating whether any attributes exist on the element, but an XDocument doesn't, as such a property is meaningless in the context of a whole XML Document. 例如 - XElement有一个HasAttributes属性,指示元素上是否存在任何属性,但XDocument不存在,因为这样的属性在整个XML文档的上下文中是没有意义的。

Here's a practical example from msdn which makes it clear. 这是msdn的一个实际例子,它清楚地说明了这一点。 Assume you have this in test.xml file: 假设你在test.xml文件中有这个:

<Root>
    <Child1>1</Child1>
    <Child2>2</Child2>
    <Child3>3</Child3>
</Root>
  1. With XDocument if you do this: 使用XDocument,如果你这样做:

     foreach (var element in XDocument.Load("test.xml").Elements()) Console.WriteLine(element); 

    You get this back: 你得到了这个:

     <Root> <Child1>1</Child1> <Child2>2</Child2> <Child3>3</Child3> </Root> 

    To get the value at Child1 node, you will have to do: 要获取Child1节点的值,您必须执行以下操作:

     var child1 = XDocument.Load("test.xml").Element("Root").Element("Child1").Value; 

    Or 要么

     var child1 = XDocument.Load("test.xml").Root.Element("Child1").Value; 
  2. With XElement if you do this: 使用XElement如果你这样做:

     foreach (var element in XElement.Load("test.xml").Elements()) Console.WriteLine(element); 

    You get this back: 你得到了这个:

     <Child1>1</Child1> <Child2>2</Child2> <Child3>3</Child3> 

    To get the value at Child1 node, you will do: 要获取Child1节点的值,您将执行以下操作:

     var child1 = XElement.Load("test.xml").Element("Child1").Value; 

In short, XElement ignores the root node while XDocument doesnt. 简而言之, XElement忽略了根节点而XDocument没有。 Roughly, XDocument.Root = XElement , or XDocument.Root.Elements() = XElement.Elements() . 粗略地说, XDocument.Root = XElement ,或XDocument.Root.Elements() = XElement.Elements() Both derive from XContainer . 两者都来自XContainer Another minor difference is that XElement implements IXmlSerializable which I dont think matters mostly. 另一个小的区别是XElement实现了IXmlSerializable ,我认为这个问题最重要。 XElement would be enough for vast majority of cases where you just want to query the sub nodes. 对于绝大多数只想查询子节点的情况, XElement就足够了。 The name confuses me though, so I prefer to use XDocument . 这个名字让我困惑,所以我更喜欢使用XDocument

From MSDN : 来自MSDN

Note that you only have to create XDocument objects if you require the specific functionality provided by the XDocument class. 请注意,如果需要XDocument类提供的特定功能,则只需创建XDocument对象。 In many circumstances, you can work directly with XElement. 在许多情况下,您可以直接使用XElement。 Working directly with XElement is a simpler programming model. 直接使用XElement是一种更简单的编程模型。

XDocument derives from XContainer. XDocument派生自XContainer。 Therefore, it can contain child nodes. 因此,它可以包含子节点。 However, XDocument objects can have only one child XElement node. 但是,XDocument对象只能有一个子XElement节点。 This reflects the XML standard that there can be only one root element in an XML document. 这反映了XML标准,即XML文档中只能有一个根元素。

According to the MSDN article LINQ to XML vs. DOM , under the subheading "Working Directly with XML Elements": 根据MSDN文章LINQ to XML vs. DOM ,在“直接使用XML元素”小标题下:

When using LINQ to XML, you use the XDocument class only if you want to add a comment or processing instruction at the root level of the document. 使用LINQ to XML时,仅当您要在文档的根级别添加注释或处理指令时才使用XDocument类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM