Share via


XDocument 建構函式

定義

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

多載

XDocument()

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

XDocument(Object[])

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

XDocument(XDocument)

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

XDocument(XDeclaration, Object[])

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

範例

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

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 ;建立 XDocument 具有一些指定初始內容的 ;以及建立 XDocument 做為另一個 XDocument 物件的複本。

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

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

XDocument()

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

public:
 XDocument();
public XDocument ();
Public Sub New ()

範例

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

XDocument doc = new XDocument();  
doc.Add(new XComment("This is a comment"));  
doc.Add(new XElement("Root", "content"));  
Console.WriteLine(doc);  
Dim doc As XDocument = New XDocument()  
doc.Add(<!--This is a comment-->)  
doc.Add(<Root>content</Root>)  
Console.WriteLine(doc)  

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

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

備註

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

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

另請參閱

適用於

XDocument(Object[])

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

public:
 XDocument(... cli::array <System::Object ^> ^ content);
public XDocument (params object[] content);
public XDocument (params object?[] content);
new System.Xml.Linq.XDocument : obj[] -> System.Xml.Linq.XDocument
Public Sub New (ParamArray content As Object())

參數

content
Object[]

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

範例

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

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 根節點來建立 XML 樹狀結構。 除非您有建立文件的特定需求 (例如,因為您必須在頂層建立處理指示與註解,或者您必須支援文件型別),否則使用 XElement 做為您的根節點通常更方便。

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

另請參閱

適用於

XDocument(XDocument)

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

public:
 XDocument(System::Xml::Linq::XDocument ^ other);
public XDocument (System.Xml.Linq.XDocument other);
new System.Xml.Linq.XDocument : System.Xml.Linq.XDocument -> System.Xml.Linq.XDocument
Public Sub New (other As XDocument)

參數

other
XDocument

要複製的 XDocument 物件。

備註

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

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

另請參閱

適用於

XDocument(XDeclaration, Object[])

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

public:
 XDocument(System::Xml::Linq::XDeclaration ^ declaration, ... cli::array <System::Object ^> ^ content);
public XDocument (System.Xml.Linq.XDeclaration declaration, params object[] content);
public XDocument (System.Xml.Linq.XDeclaration? declaration, params object[] content);
public XDocument (System.Xml.Linq.XDeclaration? declaration, params object?[] content);
new System.Xml.Linq.XDocument : System.Xml.Linq.XDeclaration * obj[] -> System.Xml.Linq.XDocument
Public Sub New (declaration As XDeclaration, ParamArray content As Object())

參數

declaration
XDeclaration

文件的 XDeclaration

content
Object[]

文件的內容。

範例

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

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"));  
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 new comment-->  
        <Root>  
            <%= From el In srcTree.<Root>.Elements _  
                Where CStr(el).StartsWith("data") _  
                Select el %>  
        </Root>  
doc.Save("Test.xml")  
Console.WriteLine(File.ReadAllText("Test.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 物件的有效內容

另請參閱

適用於