XDocument 类

定义

表示 XML 文档。Represents an XML document. 有关 XDocument 对象的组件和用法,请参阅 XDocument Class OverviewFor the components and usage of an XDocument object, see XDocument Class Overview.

public ref class XDocument : System::Xml::Linq::XContainer
public class XDocument : System.Xml.Linq.XContainer
type XDocument = class
    inherit XContainer
Public Class XDocument
Inherits XContainer
继承

示例

下面的示例创建一个文档,然后向其中添加注释和一个元素。The following example creates a document, and then adds a comment and an element to it. 然后,它使用查询结果撰写另一篇文档。It then composes another document using the results of a query.

XDocument srcTree = new XDocument(  
    new XComment("This is a comment"),  
    new XElement("Root",  
        new XElement("Child1", "data1"),  
        new XElement("Child2", "data2"),  
        new XElement("Child3", "data3"),  
        new XElement("Child2", "data4"),  
        new XElement("Info5", "info5"),  
        new XElement("Info6", "info6"),  
        new XElement("Info7", "info7"),  
        new XElement("Info8", "info8")  
    )  
);  
  
XDocument doc = new XDocument(  
    new XComment("This is a comment"),  
    new XElement("Root",  
        from el in srcTree.Element("Root").Elements()  
        where ((string)el).StartsWith("data")  
        select el  
    )  
);  
Console.WriteLine(doc);  
Dim srcTree As XDocument = _   
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>  
        <!--This is a comment-->  
        <Root>  
            <Child1>data1</Child1>  
            <Child2>data2</Child2>  
            <Child3>data3</Child3>  
            <Child2>data4</Child2>  
            <Info5>info5</Info5>  
            <Info6>info6</Info6>  
            <Info7>info7</Info7>  
            <Info8>info8</Info8>  
        </Root>  
Dim doc As XDocument = _   
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>  
        <!--This is a comment-->  
        <Root>  
            <%= From el In srcTree.<Root>.Elements _  
                Where CStr(el).StartsWith("data") _  
                Select el %>  
        </Root>  
Console.WriteLine(doc)  

该示例产生下面的输出:This example produces the following output:

<!--This is a comment-->  
<Root>  
  <Child1>data1</Child1>  
  <Child2>data2</Child2>  
  <Child3>data3</Child3>  
  <Child2>data4</Child2>  
</Root>  

注解

有关 XDocument的有效内容的详细信息,请参阅system.xml.linq.xelement> 和 XDocument 对象的有效内容For details about the valid content of an XDocument, see Valid Content of XElement and XDocument Objects.

构造函数

XDocument()

初始化 XDocument 类的新实例。Initializes a new instance of the XDocument class.

XDocument(Object[])

使用指定的内容初始化 XDocument 类的新实例。Initializes a new instance of the XDocument class with the specified content.

XDocument(XDeclaration, Object[])

用指定的 XDocument 和内容初始化 XDeclaration 类的新实例。Initializes a new instance of the XDocument class with the specified XDeclaration and content.

XDocument(XDocument)

从现有的 XDocument 对象初始化 XDocument 类的新实例。Initializes a new instance of the XDocument class from an existing XDocument object.

属性

BaseUri

获取此 XObject 的基 URI。Gets the base URI for this XObject.

(继承自 XObject)
Declaration

获取或设置此文档的 XML 声明。Gets or sets the XML declaration for this document.

Document

获取此 XDocumentXObjectGets the XDocument for this XObject.

(继承自 XObject)
DocumentType

获取此文档的文档类型定义 (DTD)。Gets the Document Type Definition (DTD) for this document.

FirstNode

获取此节点的第一个子节点。Gets the first child node of this node.

(继承自 XContainer)
LastNode

获取此节点的最后一个子节点。Gets the last child node of this node.

(继承自 XContainer)
NextNode

获取此节点的下一个同级节点。Gets the next sibling node of this node.

(继承自 XNode)
NodeType

获取此节点的节点类型。Gets the node type for this node.

Parent

获取此 XElement 的父级 XObjectGets the parent XElement of this XObject.

(继承自 XObject)
PreviousNode

获取此节点的上一个同级节点。Gets the previous sibling node of this node.

(继承自 XNode)
Root

获取此文档的 XML 树的根元素。Gets the root element of the XML Tree for this document.

方法

Add(Object)

将指定的内容添加为此 XContainer 的子级。Adds the specified content as children of this XContainer.

(继承自 XContainer)
Add(Object[])

将指定的内容添加为此 XContainer 的子级。Adds the specified content as children of this XContainer.

(继承自 XContainer)
AddAfterSelf(Object)

紧跟在此节点之后添加指定的内容。Adds the specified content immediately after this node.

(继承自 XNode)
AddAfterSelf(Object[])

紧跟在此节点之后添加指定的内容。Adds the specified content immediately after this node.

(继承自 XNode)
AddAnnotation(Object)

将对象添加到此 XObject 的批注列表。Adds an object to the annotation list of this XObject.

(继承自 XObject)
AddBeforeSelf(Object)

紧邻此节点之前添加指定的内容。Adds the specified content immediately before this node.

(继承自 XNode)
AddBeforeSelf(Object[])

紧邻此节点之前添加指定的内容。Adds the specified content immediately before this node.

(继承自 XNode)
AddFirst(Object)

将指定内容作为此文档或元素的第一个子级添加。Adds the specified content as the first children of this document or element.

(继承自 XContainer)
AddFirst(Object[])

将指定内容作为此文档或元素的第一个子级添加。Adds the specified content as the first children of this document or element.

(继承自 XContainer)
Ancestors()

返回此节点的上级元素的集合。Returns a collection of the ancestor elements of this node.

(继承自 XNode)
Ancestors(XName)

返回此节点的经过筛选的上级元素的集合。Returns a filtered collection of the ancestor elements of this node. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

(继承自 XNode)
Annotation(Type)

从此 XObject 获取指定类型的第一个批注对象。Gets the first annotation object of the specified type from this XObject.

(继承自 XObject)
Annotation<T>()

从此 XObject 获取指定类型的第一个批注对象。Gets the first annotation object of the specified type from this XObject.

(继承自 XObject)
Annotations(Type)

获取此 XObject 的指定类型的批注集合。Gets a collection of annotations of the specified type for this XObject.

(继承自 XObject)
Annotations<T>()

获取此 XObject 的指定类型的批注集合。Gets a collection of annotations of the specified type for this XObject.

(继承自 XObject)
CreateReader()

创建此节点的 XmlReaderCreates an XmlReader for this node.

(继承自 XNode)
CreateReader(ReaderOptions)

使用 readerOptions 参数指定的选项创建 XmlReaderCreates an XmlReader with the options specified by the readerOptions parameter.

(继承自 XNode)
CreateWriter()

创建可用于将节点添加至 XContainerXmlWriterCreates an XmlWriter that can be used to add nodes to the XContainer.

(继承自 XContainer)
DescendantNodes()

按文档顺序返回此文档或元素的子代节点集合。Returns a collection of the descendant nodes for this document or element, in document order.

(继承自 XContainer)
Descendants()

按文档顺序返回此文档或元素的子代元素集合。Returns a collection of the descendant elements for this document or element, in document order.

(继承自 XContainer)
Descendants(XName)

按文档顺序返回此文档或元素的已筛选的子代元素集合。Returns a filtered collection of the descendant elements for this document or element, in document order. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

(继承自 XContainer)
Element(XName)

获取具有指定的 XName 的第一个(按文档顺序)子元素。Gets the first (in document order) child element with the specified XName.

(继承自 XContainer)
Elements()

按文档顺序返回此元素或文档的子元素集合。Returns a collection of the child elements of this element or document, in document order.

(继承自 XContainer)
Elements(XName)

按文档顺序返回此元素或文档的已筛选的子元素集合。Returns a filtered collection of the child elements of this element or document, in document order. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

(继承自 XContainer)
ElementsAfterSelf()

按文档顺序返回此节点后的同级元素集合。Returns a collection of the sibling elements after this node, in document order.

(继承自 XNode)
ElementsAfterSelf(XName)

按文档顺序返回此节点后经过筛选的同级元素的集合。Returns a filtered collection of the sibling elements after this node, in document order. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

(继承自 XNode)
ElementsBeforeSelf()

按文档顺序返回此节点前的同级元素集合。Returns a collection of the sibling elements before this node, in document order.

(继承自 XNode)
ElementsBeforeSelf(XName)

按文档顺序返回此节点前经过筛选的同级元素的集合。Returns a filtered collection of the sibling elements before this node, in document order. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

(继承自 XNode)
Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IsAfter(XNode)

确定当前节点是否按文档顺序显示在指定节点之后。Determines if the current node appears after a specified node in terms of document order.

(继承自 XNode)
IsBefore(XNode)

确定当前节点是否按文档顺序显示在指定节点之前。Determines if the current node appears before a specified node in terms of document order.

(继承自 XNode)
Load(Stream)

使用指定的流创建一个新的 XDocument 实例。Creates a new XDocument instance by using the specified stream.

Load(Stream, LoadOptions)

使用指定流创建新的 XDocument 实例,也可以选择保留空白,设置基 URI 和保留行信息。Creates a new XDocument instance by using the specified stream, optionally preserving white space, setting the base URI, and retaining line information.

Load(String)

从文件创建新 XDocumentCreates a new XDocument from a file.

Load(String, LoadOptions)

从文件创建新 XDocument,还可以选择保留空白和行信息以及设置基 URI。Creates a new XDocument from a file, optionally preserving white space, setting the base URI, and retaining line information.

Load(TextReader)

XDocument 创建新的 TextReaderCreates a new XDocument from a TextReader.

Load(TextReader, LoadOptions)

XDocument 创建新 TextReader,还可以选择保留空白和行信息以及设置基 URI。Creates a new XDocument from a TextReader, optionally preserving white space, setting the base URI, and retaining line information.

Load(XmlReader)

XDocument 创建新 XmlReaderCreates a new XDocument from an XmlReader.

Load(XmlReader, LoadOptions)

XDocument 加载 XmlReader,还可以选择设置基 URI 和保留行信息。Loads an XDocument from an XmlReader, optionally setting the base URI, and retaining line information.

LoadAsync(Stream, LoadOptions, CancellationToken)

以异步方式创建新的 XDocument 并从指定的流初始化其基础 XML 树,可以选择保留空格。Asynchronously creates a new XDocument and initializes its underlying XML tree from the specified stream, optionally preserving white space.

LoadAsync(TextReader, LoadOptions, CancellationToken)

创建新的 XDocument 并使用指定的 TextReader 参数初始化其基础 XML 树,可以选择保留空格。Creates a new XDocument and initializes its underlying XML tree using the specified TextReader parameter, optionally preserving white space.

LoadAsync(XmlReader, LoadOptions, CancellationToken)

创建包含指定的 XDocument 的内容的新 XmlReaderCreates a new XDocument containing the contents of the specified XmlReader.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Nodes()

按文档顺序返回此元素或文档的子节点集合。Returns a collection of the child nodes of this element or document, in document order.

(继承自 XContainer)
NodesAfterSelf()

按文档顺序返回此节点后的同级节点的集合。Returns a collection of the sibling nodes after this node, in document order.

(继承自 XNode)
NodesBeforeSelf()

按文档顺序返回此节点前的同级节点的集合。Returns a collection of the sibling nodes before this node, in document order.

(继承自 XNode)
Parse(String)

从字符串创建新 XDocumentCreates a new XDocument from a string.

Parse(String, LoadOptions)

从字符串创建新 XDocument,还可以选择保留空白和行信息以及设置基 URI。Creates a new XDocument from a string, optionally preserving white space, setting the base URI, and retaining line information.

Remove()

从节点父级中删除此节点。Removes this node from its parent.

(继承自 XNode)
RemoveAnnotations(Type)

从此 XObject 移除指定类型的批注。Removes the annotations of the specified type from this XObject.

(继承自 XObject)
RemoveAnnotations<T>()

从此 XObject 移除指定类型的批注。Removes the annotations of the specified type from this XObject.

(继承自 XObject)
RemoveNodes()

从此文档或元素中移除子节点。Removes the child nodes from this document or element.

(继承自 XContainer)
ReplaceNodes(Object)

使用指定内容替换此文档或元素的子节点。Replaces the children nodes of this document or element with the specified content.

(继承自 XContainer)
ReplaceNodes(Object[])

使用指定内容替换此文档或元素的子节点。Replaces the children nodes of this document or element with the specified content.

(继承自 XContainer)
ReplaceWith(Object)

将此节点替换为指定的内容。Replaces this node with the specified content.

(继承自 XNode)
ReplaceWith(Object[])

将此节点替换为指定的内容。Replaces this node with the specified content.

(继承自 XNode)
Save(Stream)

将此 XDocument 输出到指定的 StreamOutputs this XDocument to the specified Stream.

Save(Stream, SaveOptions)

将此 XDocument 输出到指定的 Stream,(可选)并指定格式设置行为。Outputs this XDocument to the specified Stream, optionally specifying formatting behavior.

Save(String)

序列化此 XDocument 到文件,如果该文件存在,则覆盖现有的文件。Serialize this XDocument to a file, overwriting an existing file, if it exists.

Save(String, SaveOptions)

将此 XDocument 序列化为文件,还可以选择禁用格式设置。Serialize this XDocument to a file, optionally disabling formatting.

Save(TextWriter)

将此 XDocument 序列化为 TextWriterSerialize this XDocument to a TextWriter.

Save(TextWriter, SaveOptions)

将此 XDocument 序列化为 TextWriter,还可以选择禁用格式设置。Serialize this XDocument to a TextWriter, optionally disabling formatting.

Save(XmlWriter)

将此 XDocument 序列化为 XmlWriterSerialize this XDocument to an XmlWriter.

SaveAsync(Stream, SaveOptions, CancellationToken)

将此 XDocument 输出到 StreamOutput this XDocument to a Stream.

SaveAsync(TextWriter, SaveOptions, CancellationToken)

将此 XDocument 写入到 TextWriterWrites this XDocument to a TextWriter.

SaveAsync(XmlWriter, CancellationToken)

将此 XDocument 写入到 XmlWriterWrites this XDocument to an XmlWriter.

ToString()

返回此节点的缩进 XML。Returns the indented XML for this node.

(继承自 XNode)
ToString(SaveOptions)

返回此节点的 XML,还可以选择禁用格式设置。Returns the XML for this node, optionally disabling formatting.

(继承自 XNode)
WriteTo(XmlWriter)

将此文档写入 XmlWriterWrite this document to an XmlWriter.

WriteToAsync(XmlWriter, CancellationToken)

将此 XDocument 的基础 XML 树写入指定的 XmlWriterWrites this XDocument's underlying XML tree to the specified XmlWriter.

事件

Changed

在此 XObject 或其任何子代发生了更改时引发。Raised when this XObject or any of its descendants have changed.

(继承自 XObject)
Changing

当此 XObject 或其任何子代要发生更改时引发。Raised when this XObject or any of its descendants are about to change.

(继承自 XObject)

显式接口实现

IXmlLineInfo.HasLineInfo()

获取一个值,该值指示此 XObject 是否具有行信息。Gets a value indicating whether or not this XObject has line information.

(继承自 XObject)
IXmlLineInfo.LineNumber

获取基础 XmlReader 为此 XObject 报告的行号。Gets the line number that the underlying XmlReader reported for this XObject.

(继承自 XObject)
IXmlLineInfo.LinePosition

获取基础 XmlReader 为此 XObject 报告的行位置。Gets the line position that the underlying XmlReader reported for this XObject.

(继承自 XObject)

扩展方法

Validate(XDocument, XmlSchemaSet, ValidationEventHandler)

此方法验证 XDocument 是否与 XmlSchemaSet 中的 XSD 相符。This method validates that an XDocument conforms to an XSD in an XmlSchemaSet.

Validate(XDocument, XmlSchemaSet, ValidationEventHandler, Boolean)

验证 XDocument 是否与 XmlSchemaSet 中的某个 XSD 相符,并且可以选择使用架构验证后信息集 (PSVI) 填充 XML 树。Validates that an XDocument conforms to an XSD in an XmlSchemaSet, optionally populating the XML tree with the post-schema-validation infoset (PSVI).

CreateNavigator(XNode)

XPathNavigator 创建一个 XNodeCreates an XPathNavigator for an XNode.

CreateNavigator(XNode, XmlNameTable)

XPathNavigator 创建一个 XNodeCreates an XPathNavigator for an XNode. XmlNameTable 可以使 XPath 表达式的处理变得更高效。The XmlNameTable enables more efficient XPath expression processing.

XPathEvaluate(XNode, String)

计算 XPath 表达式。Evaluates an XPath expression.

XPathEvaluate(XNode, String, IXmlNamespaceResolver)

计算 XPath 表达式,使用指定的 IXmlNamespaceResolver 解析命名空间前缀。Evaluates an XPath expression, resolving namespace prefixes using the specified IXmlNamespaceResolver.

XPathSelectElement(XNode, String)

使用 XPath 表达式选择 XElementSelects an XElement using a XPath expression.

XPathSelectElement(XNode, String, IXmlNamespaceResolver)

使用 XPath 表达式选择 XElement,并使用指定的 IXmlNamespaceResolver 解析命名空间前缀。Selects an XElement using a XPath expression, resolving namespace prefixes using the specified IXmlNamespaceResolver.

XPathSelectElements(XNode, String)

使用 XPath 表达式选择一个元素集合。Selects a collection of elements using an XPath expression.

XPathSelectElements(XNode, String, IXmlNamespaceResolver)

使用 XPath 表达式选择一个元素集合,并使用指定的 IXmlNamespaceResolver 解析命名空间前缀。Selects a collection of elements using an XPath expression, resolving namespace prefixes using the specified IXmlNamespaceResolver.

ToXPathNavigable(XNode)

返回一个访问器,该访问器可用于导航和编辑指定的 XNodeReturns an accessor that allows you to navigate and edit the specified XNode.

适用于

另请参阅