XDocument 類別
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示 XML 文件。 如需 XDocument 物件的元件和用法,請參閱 XDocument 類別概觀。
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
- 繼承
下列範例會建立文件,然後將批注和元素加入其中。 然後,它會使用查詢的結果來撰寫另一份檔。
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 is a comment-->
<Root>
<Child1>data1</Child1>
<Child2>data2</Child2>
<Child3>data3</Child3>
<Child2>data4</Child2>
</Root>
如需 有效內容 XDocument的詳細資訊,請參閱 XElement 和 XDocument 物件的有效內容。
XDocument() |
初始化 XDocument 類別的新執行個體。 |
XDocument(Object[]) |
使用指定的內容初始化 XDocument 類別的新執行個體。 |
XDocument(XDeclaration, Object[]) |
使用指定的 XDocument 和內容,初始化 XDeclaration 類別的新執行個體。 |
XDocument(XDocument) |
Base |
取得此 XObject 的基底 URI。 (繼承來源 XObject) |
Declaration |
取得或設定這個文件的 XML 宣告。 |
Document | (繼承來源 XObject) |
Document |
取得這個文件的文件類型定義 (DTD)。 |
First |
取得此節點的第一個子節點。 (繼承來源 XContainer) |
Last |
取得此節點的最後一個子節點。 (繼承來源 XContainer) |
Next |
取得這個節點的下一個同層級 (Sibling) 節點。 (繼承來源 XNode) |
Node |
取得此節點的節點類型。 |
Parent | (繼承來源 XObject) |
Previous |
取得這個節點的上一個同層級節點。 (繼承來源 XNode) |
Root |
取得此文件之 XML 樹狀的根項目 (Root Element)。 |
Changed |
當此 XObject 或其任何子代發生變更時會引發。 (繼承來源 XObject) |
Changing |
當此 XObject 或其任何子代發生變更時會引發。 (繼承來源 XObject) |
IXml |
取得值,指出此 XObject 是否具有行資訊。 (繼承來源 XObject) |
IXml |
取得基礎 XmlReader 所回報此 XObject 的行號。 (繼承來源 XObject) |
IXml |
取得基礎 XmlReader 所報告之此 XObject 的行位置。 (繼承來源 XObject) |
Validate(XDocument, Xml |
這個方法會驗證 XDocument 是否符合 XmlSchemaSet 中的 XSD。 |
Validate(XDocument, Xml |
驗證 XDocument 是否符合 XmlSchemaSet 中的 XSD,並選擇性地將後結構描述驗證資訊集 (PSVI) 填入 XML 樹狀目錄。 |
Create |
建立 XPathNavigator 的 XNode。 |
Create |
建立 XPathNavigator 的 XNode。 XmlNameTable 可讓 XPath 運算式處理時更有效率。 |
XPath |
評估 XPath 運算式。 |
XPath |
評估 XPath 運算式,並使用指定的 IXmlNamespaceResolver 解析命名空間後置字元。 |
XPath |
使用 XPath 運算式選取 XElement。 |
XPath |
使用 XPath 運算式選取 XElement,並使用指定的 IXmlNamespaceResolver 解析命名空間後置字元。 |
XPath |
使用 XPath 運算式選取項目的集合。 |
XPath |
使用 XPath 運算式選取項目集合,並使用指定的 IXmlNamespaceResolver 解析命名空間後置字元。 |
To |
傳回存取子,讓您巡覽及編輯指定的 XNode。 |
產品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |