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 具有一些指定的初始内容;并创建另一 XDocumentXDocument 对象的副本。

没有多少方案需要创建 XDocument。 相反,通常可以使用 XElement 根节点创建 XML 树。 除非有创建文档的特定要求(例如,因为必须在顶层创建处理指令和注释,或者必须支持文档类型),否则通常更方便用作 XElement 根节点。

有关有效 XDocument内容的详细信息,请参阅 XElement 和 XDocument 对象的有效内容

XDocument()

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
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[])

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
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)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
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[])

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs
Source:
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

文档的一个 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 对象的有效内容

另请参阅

适用于