XDocument コンストラクター
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
XDocument クラスの新しいインスタンスを初期化します。
オーバーロード
| 名前 | 説明 |
|---|---|
| XDocument() |
XDocument クラスの新しいインスタンスを初期化します。 |
| XDocument(Object[]) |
指定したコンテンツを使用して、 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
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)
パラメーター
注釈
このコンストラクターを使用して、 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
ドキュメントの 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 オブジェクトの有効なコンテンツ」を参照してください。