XDocument 建構函式

定義

初始化 XDocument 類別的新執行個體。

多載

XDocument()

初始化 XDocument 類別的新執行個體。

XDocument(Object[])

使用指定的內容初始化 XDocument 類別的新執行個體。

XDocument(XDocument)

從現有的 XDocument 物件,初始化 XDocument 類別的新執行個體。

XDocument(XDeclaration, Object[])

使用指定的 XDocument 和內容,初始化 XDeclaration 類別的新執行個體。

範例

下列範例會建立檔,然後將批註和元素加入其中。 然後,它會使用查詢的結果來撰寫另一份檔。

C#
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);  

這個範例會產生下列輸出:

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

備註

多載建構函式可讓您建立新的空白 XDocument ;建立 XDocument 具有一些指定初始內容的 ;以及建立 XDocument 做為另一個 XDocument 物件的複本。

需要您建立 XDocument 的案例並不多。 不過,您通常可以使用 XElement 根節點來建立 XML 樹狀結構。 除非您有建立文件的特定需求 (例如,因為您必須在頂層建立處理指示與註解,或者您必須支援文件型別),否則使用 XElement 做為您的根節點通常更方便。

如需 有效內容 XDocument 的詳細資訊,請參閱 XElement 和 XDocument 物件的有效內容

XDocument()

初始化 XDocument 類別的新執行個體。

C#
public XDocument ();

範例

下列範例會建立新的檔,然後將批註和元素加入其中。

C#
XDocument doc = new XDocument();  
doc.Add(new XComment("This is a comment"));  
doc.Add(new XElement("Root", "content"));  
Console.WriteLine(doc);  

這個範例會產生下列輸出:

XML
<!--This is a comment-->  
<Root>content</Root>  

備註

需要您建立 XDocument 的案例並不多。 不過,您通常可以使用 XElement 根節點來建立 XML 樹狀結構。 除非您有建立文件的特定需求 (例如,因為您必須在頂層建立處理指示與註解,或者您必須支援文件型別),否則使用 XElement 做為您的根節點通常更方便。

如需 有效內容 XDocument 的詳細資訊,請參閱 XElement 和 XDocument 物件的有效內容

另請參閱

適用於

.NET 7 和其他版本
產品 版本
.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
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

XDocument(Object[])

使用指定的內容初始化 XDocument 類別的新執行個體。

C#
public XDocument (params object[] content);
C#
public XDocument (params object?[] content);

參數

content
Object[]

要加入此文件之內容物件的參數清單。

範例

下列範例會建立檔,然後將批註和元素加入其中。 然後,它會使用查詢的結果來撰寫另一份檔。

C#
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);  

這個範例會產生下列輸出:

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

備註

需要您建立 XDocument 的案例並不多。 不過,您通常可以使用 XElement 根節點來建立 XML 樹狀結構。 除非您有建立文件的特定需求 (例如,因為您必須在頂層建立處理指示與註解,或者您必須支援文件型別),否則使用 XElement 做為您的根節點通常更方便。

如需 有效內容 XDocument 的詳細資訊,請參閱 XElement 和 XDocument 物件的有效內容

另請參閱

適用於

.NET 7 和其他版本
產品 版本
.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
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

XDocument(XDocument)

從現有的 XDocument 物件,初始化 XDocument 類別的新執行個體。

C#
public XDocument (System.Xml.Linq.XDocument other);

參數

other
XDocument

要複製的 XDocument 物件。

備註

您可以使用這個建構函式來建立 的 XDocument 深層複本。

這個建構函式會周遊 參數所 other 指定檔中的所有節點和屬性,並在其組合新初始化 XDocument 時建立所有節點的複本。

另請參閱

適用於

.NET 7 和其他版本
產品 版本
.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
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

XDocument(XDeclaration, Object[])

使用指定的 XDocument 和內容,初始化 XDeclaration 類別的新執行個體。

C#
public XDocument (System.Xml.Linq.XDeclaration declaration, params object[] content);
C#
public XDocument (System.Xml.Linq.XDeclaration? declaration, params object[] content);
C#
public XDocument (System.Xml.Linq.XDeclaration? declaration, params object?[] content);

參數

declaration
XDeclaration

文件的 XDeclaration

content
Object[]

文件的內容。

範例

下列範例會使用此建構函式來建立檔。

C#
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 XDeclaration("1.0", "utf-8", "yes"),  
    new XComment("This is a new comment"),  
    new XElement("Root",  
        from el in srcTree.Element("Root").Elements()  
        where ((string)el).StartsWith("data")  
        select el  
    )  
);  
doc.Save("Test.xml");  
Console.WriteLine(File.ReadAllText("Test.xml"));  

這個範例會產生下列輸出:

XML
<?xml version="1.0" encoding="utf-8" standalone="yes"?>  
<!--This is a new comment-->  
<Root>  
  <Child1>data1</Child1>  
  <Child2>data2</Child2>  
  <Child3>data3</Child3>  
  <Child2>data4</Child2>  
</Root>  

備註

需要您建立 XDocument 的案例並不多。 不過,您通常可以使用 XElement 根節點來建立 XML 樹狀結構。 除非您有建立文件的特定需求 (例如,因為您必須在頂層建立處理指示與註解,或者您必須支援文件型別),否則使用 XElement 做為您的根節點通常更方便。

如需 有效內容 XDocument 的詳細資訊,請參閱 XElement 和 XDocument 物件的有效內容

另請參閱

適用於

.NET 7 和其他版本
產品 版本
.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
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0