XDocument 類別

定義

表示 XML 文件。Represents an XML document. 如需 XDocument 物件的元件和用法,請參閱 XDocument 類別概觀For 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

取得這個節點的下一個同層級 (Sibling) 節點。Gets the next sibling node of this node.

(繼承來源 XNode)
NodeType

取得此節點的節點類型。Gets the node type for this node.

Parent

取得這個 XElementXObjectGets the parent XElement of this XObject.

(繼承來源 XObject)
PreviousNode

取得這個節點的上一個同層級節點。Gets the previous sibling node of this node.

(繼承來源 XNode)
Root

取得此文件之 XML 樹狀結構的根項目 (Root Element)。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()

建立可以用來將節點加入到 XmlWriterXContainerCreates 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,其中包含指定 XmlReader 的內容。Creates 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()

將這個節點從其父代 (Parent) 移除。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)

建立 XPathNavigatorXNodeCreates an XPathNavigator for an XNode.

CreateNavigator(XNode, XmlNameTable)

建立 XPathNavigatorXNodeCreates 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.

適用於

另請參閱