XDocument 建構函式

定義

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

多載

名稱 Description
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.cs
來源:
XDocument.cs
來源:
XDocument.cs
來源:
XDocument.cs
來源:
XDocument.cs

初始化 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.cs
來源:
XDocument.cs
來源:
XDocument.cs
來源:
XDocument.cs
來源:
XDocument.cs

初始化一個包含指定內容的新 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.cs
來源:
XDocument.cs
來源:
XDocument.cs
來源:
XDocument.cs
來源:
XDocument.cs

從現有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.cs
來源:
XDocument.cs
來源:
XDocument.cs
來源:
XDocument.cs
來源:
XDocument.cs

初始化一個新的類別實例 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

文件的 A 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 物件的有效內容

另請參閱

適用於