XElement コンストラクター

定義

XElement クラスの新しいインスタンスを初期化します。

オーバーロード

名前 説明
XElement(XElement)

別のXElement オブジェクトからXElement クラスの新しいインスタンスを初期化します。

XElement(XName)

指定した名前を使用して、 XElement クラスの新しいインスタンスを初期化します。

XElement(XStreamingElement)

XElement オブジェクトからXStreamingElement クラスの新しいインスタンスを初期化します。

XElement(XName, Object)

指定した名前とコンテンツを使用して、 XElement クラスの新しいインスタンスを初期化します。

XElement(XName, Object[])

指定した名前とコンテンツを使用して、 XElement クラスの新しいインスタンスを初期化します。

次の例では、XML ツリーを作成します。 新しい要素の内容は、LINQ クエリから取得されます。

XElement xmlTree1 = new XElement("Root",
    new XElement("Child", 1),
    new XElement("Child", 2),
    new XElement("Child", 3),
    new XElement("Child", 4),
    new XElement("Child", 5),
    new XElement("Child", 6)
);

XElement xmlTree2 = new XElement("Root",
    from el in xmlTree1.Elements()
    where((int)el >= 3 && (int)el <= 5)
    select el
);
Console.WriteLine(xmlTree2);
Dim xmlTree1 As XElement = _
        <Root>
            <Child>1</Child>
            <Child>2</Child>
            <Child>3</Child>
            <Child>4</Child>
            <Child>5</Child>
            <Child>6</Child>
        </Root>

Dim xmlTree2 As XElement = _
    <Root>
        <%= From el In xmlTree1.Elements() _
            Where el.Value >= 3 And el.Value <= 5 _
            Select el %>
    </Root>

Console.WriteLine(xmlTree2)

この例を実行すると、次の出力が生成されます。

<Root>
  <Child>3</Child>
  <Child>4</Child>
  <Child>5</Child>
</Root>

注釈

このコンストラクターに渡すことができる有効なコンテンツの詳細については、「 XElement オブジェクトと XDocument オブジェクトの有効なコンテンツ」を参照してください。

文字列から XNameへの暗黙的な変換があります。 このコンストラクターの一般的な用途は、新しい XNameを作成するのではなく、パラメーターとして文字列を指定することです。

名前空間に要素を作成する場合、一般的な用途は、 XNamespace と文字列を使用して加算演算子オーバーロードを使用して XNameを作成することです。 詳細については、「 XML 名前空間の操作」を参照してください。

XElement(XElement)

ソース:
XElement.cs
ソース:
XElement.cs
ソース:
XElement.cs
ソース:
XElement.cs
ソース:
XElement.cs

別のXElement オブジェクトからXElement クラスの新しいインスタンスを初期化します。

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

パラメーター

other
XElement

コピー元の XElement オブジェクト。

次の例では、XML ツリーを作成し、ツリーの複製を作成してから、 DeepEqualsを呼び出して、2 つの XML ツリーが等しいかどうかをテストします。

XElement xmlTree = new XElement("Root",
    new XAttribute("Att1", 1),
    new XElement("Child1", 1),
    new XElement("Child2", 2)
);

// Create a clone of the tree.
XElement treeClone = new XElement(xmlTree);

Console.WriteLine("xmlTree = treeClone: {0}", XNode.DeepEquals(xmlTree, treeClone));

// Do some work with xmlTree, perhaps pass it to other methods.
xmlTree.Add(new XElement("Child3", 3));

Console.WriteLine("xmlTree = treeClone: {0}", XNode.DeepEquals(xmlTree, treeClone));
Dim xmlTree As XElement = _
        <Root Att1="1">
            <Child1>1</Child1>
            <Child2>2</Child2>
        </Root>

' Create a clone of the tree.
Dim treeClone As XElement = New XElement(xmlTree)

Console.WriteLine("xmlTree = treeClone: {0}", XNode.DeepEquals(xmlTree, treeClone))

' Do some work with xmlTree, perhaps pass it to other methods.
xmlTree.Add(New XElement("Child3", 3))

Console.WriteLine("xmlTree = treeClone: {0}", XNode.DeepEquals(xmlTree, treeClone))

この例を実行すると、次の出力が生成されます。

xmlTree = treeClone: True
xmlTree = treeClone: False

注釈

このコンストラクターは、要素のディープ コピーを作成します。

こちらもご覧ください

適用対象

XElement(XName)

ソース:
XElement.cs
ソース:
XElement.cs
ソース:
XElement.cs
ソース:
XElement.cs
ソース:
XElement.cs

指定した名前を使用して、 XElement クラスの新しいインスタンスを初期化します。

public:
 XElement(System::Xml::Linq::XName ^ name);
public XElement(System.Xml.Linq.XName name);
new System.Xml.Linq.XElement : System.Xml.Linq.XName -> System.Xml.Linq.XElement
Public Sub New (name As XName)

パラメーター

name
XName

要素の名前を含む XName

次の例では、コンテンツのない要素を作成します。

XElement el = new XElement("Root");
Console.WriteLine(el);
Dim el As XElement = <Root/>
Console.WriteLine(el)

この例を実行すると、次の出力が生成されます。

<Root />

次の例では、コンテンツのない名前空間に要素を作成します。 詳細については、「 XML 名前空間の操作」を参照してください。

XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root");
Console.WriteLine(root);
Imports <xmlns="http://www.adventure-works.com">

Module Module1
    Sub Main()
        Dim root = <Root/>
        Console.WriteLine(root)
    End Sub
End Module

この例を実行すると、次の出力が生成されます。

<Root xmlns="http://www.adventure-works.com" />

注釈

このコンストラクターは、コンテンツと属性を持たない要素を作成します。

文字列から XNameへの暗黙的な変換があります。 このコンストラクターの一般的な用途は、新しい XNameを作成するのではなく、パラメーターとして文字列を指定することです。 名前空間に要素を作成する場合、一般的な用途は、 XNamespace と文字列を使用して加算演算子オーバーロードを使用して XNameを作成することです。 詳細については、「 XML 名前空間の操作」を参照してください。

こちらもご覧ください

適用対象

XElement(XStreamingElement)

ソース:
XElement.cs
ソース:
XElement.cs
ソース:
XElement.cs
ソース:
XElement.cs
ソース:
XElement.cs

XElement オブジェクトからXStreamingElement クラスの新しいインスタンスを初期化します。

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

パラメーター

other
XStreamingElement

このXStreamingElementの内容に対して反復処理される未評価のクエリを含むXElement

次の例では、ソース XML ツリーを作成し、ソース XML ツリーのクエリから XStreamingElement を作成します。 次に、 XStreamingElement をコンソールにシリアル化し、ソース XML ツリーに新しい要素を追加してから、 XStreamingElement をもう一度シリアル化します。 ソース XML ツリーに新しく追加された要素が最初のシリアル化に含まれていないが、2 番目のシリアル化に含まれていることがわかります。

XElement src = new XElement("Root",
                   new XElement("Child1", 1),
                   new XElement("Child2", 2),
                   new XElement("Child3", 3)
               );
XStreamingElement xse = new XStreamingElement("NewRoot",
                            from el in src.Elements()
                            where (int)el >= 2
                            select el
                        );
Console.WriteLine(xse);
src.Add(new XElement("Child4", 4));
Console.WriteLine("----");
Console.WriteLine(xse);
Dim src As XElement = _
        <Root>
            <Child1>1</Child1>
            <Child2>2</Child2>
            <Child3>3</Child3>
        </Root>
Dim xse As XStreamingElement = New XStreamingElement("NewRoot", _
        From el In src.Elements() _
        Where (CInt(el) >= 2) _
        Select el _
)
Console.WriteLine(xse)
src.Add(New XElement("Child4", 4))
Console.WriteLine("----")
Console.WriteLine(xse)

この例を実行すると、次の出力が生成されます。

<NewRoot>
  <Child2>2</Child2>
  <Child3>3</Child3>
</NewRoot>
----
<NewRoot>
  <Child2>2</Child2>
  <Child3>3</Child3>
  <Child4>4</Child4>
</NewRoot>

注釈

このコンストラクターは、指定した XStreamingElementの内容を反復処理し、その内容を持つ要素を作成します。

こちらもご覧ください

適用対象

XElement(XName, Object)

ソース:
XElement.cs
ソース:
XElement.cs
ソース:
XElement.cs
ソース:
XElement.cs
ソース:
XElement.cs

指定した名前とコンテンツを使用して、 XElement クラスの新しいインスタンスを初期化します。

public:
 XElement(System::Xml::Linq::XName ^ name, System::Object ^ content);
public XElement(System.Xml.Linq.XName name, object content);
public XElement(System.Xml.Linq.XName name, object? content);
new System.Xml.Linq.XElement : System.Xml.Linq.XName * obj -> System.Xml.Linq.XElement
Public Sub New (name As XName, content As Object)

パラメーター

name
XName

要素名を含む XName

content
Object

要素の内容。

次の例では、XML ツリーを作成します。 新しい要素の内容は、LINQ クエリから取得されます。

XElement xmlTree1 = new XElement("Root",
    new XElement("Child1", 1),
    new XElement("Child2", 2),
    new XElement("Child3", 3),
    new XElement("Child4", 4),
    new XElement("Child5", 5),
    new XElement("Child6", 6)
);

XElement xmlTree2 = new XElement("Root",
    from el in xmlTree1.Elements()
    where((int)el >= 3 && (int)el <= 5)
    select el
);
Console.WriteLine(xmlTree2);
Dim xmlTree1 As XElement = _
        <Root>
            <Child1>1</Child1>
            <Child2>2</Child2>
            <Child3>3</Child3>
            <Child4>4</Child4>
            <Child5>5</Child5>
            <Child6>6</Child6>
        </Root>

Dim xmlTree2 As XElement = _
    <Root>
        <%= From el In xmlTree1.Elements() _
            Where el.Value >= 3 And el.Value <= 5 _
            Select el %>
    </Root>

Console.WriteLine(xmlTree2)

この例を実行すると、次の出力が生成されます。

<Root>
  <Child3>3</Child3>
  <Child4>4</Child4>
  <Child5>5</Child5>
</Root>

次の例では、さまざまな種類のコンテンツを含む XML ツリーを作成します。

XElement root;

// String content:
root = new XElement("Root", "Some text");
Console.WriteLine(root);

// XElement object content:
root = new XElement("Root",
    new XElement("NewChild", "n")
);
Console.WriteLine(root);

// XAttribute object content:
root = new XElement("Root",
    new XAttribute("NewAttribute", "n")
);
Console.WriteLine(root);

// Double content:
double dbl = 12.345;
root = new XElement("Root", dbl);
Console.WriteLine(root);

// DateTime content:
DateTime dt = new DateTime(2006, 10, 6, 12, 30, 00);
root = new XElement("Root", dt);
Console.WriteLine(root);

// String array content:
// Any collection other than a collection of XElement or XAttribute objects
// are converted to strings. The strings are concatenated and added.
string[] stringArray = {
    "abc",
    "def",
    "ghi"
};
root = new XElement("Root", stringArray);
Console.WriteLine(root);

// XElement object array content:
XElement[] ellArray = {
    new XElement("NewChild1", 1),
    new XElement("NewChild2", 2),
    new XElement("NewChild3", 3)
};
root = new XElement("Root", ellArray);
Console.WriteLine(root);

// XAttribute object array content:
XAttribute[] attArray = {
    new XAttribute("NewAtt1", 1),
    new XAttribute("NewAtt2", 2),
    new XAttribute("NewAtt3", 3)
};
root = new XElement("Root", attArray);
Console.WriteLine(root);
Dim root As XElement

' String content:
root = <Root>Some text</Root>
Console.WriteLine(root)

' XElement object content:
root = <Root>
           <NewChild>n</NewChild>
       </Root>
Console.WriteLine(root)

' XAttribute object content:
root = <Root NewAttribute="n"/>
Console.WriteLine(root)

' Double content:
Dim dbl As Double = 12.345
root = <Root><%= dbl %></Root>
Console.WriteLine(root)

' DateTime content:
Dim dt As DateTime = New DateTime(2006, 10, 6, 12, 30, 0)
root = <Root><%= dt %></Root>
Console.WriteLine(root)

' String array content:
' Any collection other than a collection of XElement or XAttribute objects
' are converted to strings. The strings are concatenated and added.

Dim stringArray As String() = { _
    "abc", _
    "def", _
    "ghi" _
}
root = <Root><%= stringArray %></Root>
Console.WriteLine(root)

' XElement object array content:
Dim ellArray As XElement() = { _
    <NewChild1>1</NewChild1>, _
    <NewChild2>2</NewChild2>, _
    <NewChild3>3</NewChild3> _
}

root = <Root><%= ellArray %></Root>
Console.WriteLine(root)

' XAttribute object array content
Dim attArray As XAttribute() = { _
    New XAttribute("NewAtt1", 1), _
    New XAttribute("NewAtt2", 2), _
    New XAttribute("NewAtt3", 3) _
}
root = <Root><%= attArray %></Root>
Console.WriteLine(root)

この例を実行すると、次の出力が生成されます。

<Root>Some text</Root>
<Root>
  <NewChild>n</NewChild>
</Root>
<Root NewAttribute="n" />
<Root>12.345</Root>
<Root>2006-10-06T12:30:00</Root>
<Root>abcdefghi</Root>
<Root>
  <NewChild1>1</NewChild1>
  <NewChild2>2</NewChild2>
  <NewChild3>3</NewChild3>
</Root>
<Root NewAtt1="1" NewAtt2="2" NewAtt3="3" />

次の例では、名前空間に XML ツリーを作成します。

// Create an XML tree in a namespace.
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
    new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);
' Create an XML tree in a namespace.
Dim root As XElement = _
    <Root xmlns='http://www.adventure-works.com'>
        <Child>child content</Child>
    </Root>
Console.WriteLine(root)

この例を実行すると、次の出力が生成されます。

<Root xmlns="http://www.adventure-works.com">
  <Child>child content</Child>
</Root>

次の例では、入れ子になった名前空間を含む XML ツリーを作成します。

// Create an XML tree with nested namespaces.
XNamespace aw = "http://www.adventure-works.com";
XNamespace fc = "www.fourthcoffee.com";
XDocument root = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XElement(aw + "Root",
        new XElement(fc + "Child",
            new XElement(aw + "DifferentChild", "other content")
        )
    )
);
Console.WriteLine(root);
' Create an XML tree with nested namespaces.
Dim root As XDocument = _
    <?xml version='1.0'?>
    <Root xmlns='http://www.adventure-works.com'>
        <Child xmlns='www.fourthcoffee.com'>
        <DifferentChild xmlns='http://www.adventure-works.com'>other content</DifferentChild>
        </Child>
    </Root>
Console.WriteLine(root)

この例を実行すると、次の出力が生成されます。

<Root xmlns="http://www.adventure-works.com">
  <Child xmlns="www.fourthcoffee.com">
    <DifferentChild xmlns="http://www.adventure-works.com">other content</DifferentChild>
  </Child>
</Root>

注釈

このコンストラクターは、指定されたコンテンツと属性を持つ要素を作成します。

文字列から XNameへの暗黙的な変換があります。 このコンストラクターの一般的な用途は、新しい XNameを作成するのではなく、パラメーターとして文字列を指定することです。

名前空間に要素を作成する場合、一般的な用途は、 XNamespace と文字列を使用して加算演算子オーバーロードを使用して XNameを作成することです。 詳細については、「 XML 名前空間の操作」を参照してください。

このコンストラクターに渡すことができる有効なコンテンツの詳細については、「 XElement オブジェクトと XDocument オブジェクトの有効なコンテンツ」を参照してください。

こちらもご覧ください

適用対象

XElement(XName, Object[])

ソース:
XElement.cs
ソース:
XElement.cs
ソース:
XElement.cs
ソース:
XElement.cs
ソース:
XElement.cs

指定した名前とコンテンツを使用して、 XElement クラスの新しいインスタンスを初期化します。

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

パラメーター

name
XName

要素名を含む XName

content
Object[]

要素の初期コンテンツ。

次の例では、XML ツリーを作成します。 新しい要素の内容は、LINQ クエリから取得されます。

XElement xmlTree1 = new XElement("Root",
    new XElement("Child1", 1),
    new XElement("Child2", 2),
    new XElement("Child3", 3),
    new XElement("Child4", 4),
    new XElement("Child5", 5),
    new XElement("Child6", 6)
);

XElement xmlTree2 = new XElement("Root",
    from el in xmlTree1.Elements()
    where((int)el >= 3 && (int)el <= 5)
    select el
);
Console.WriteLine(xmlTree2);
Dim xmlTree1 As XElement = _
        <Root>
            <Child1>1</Child1>
            <Child2>2</Child2>
            <Child3>3</Child3>
            <Child4>4</Child4>
            <Child5>5</Child5>
            <Child6>6</Child6>
        </Root>

Dim xmlTree2 As XElement = _
    <Root>
        <%= From el In xmlTree1.Elements() _
            Where el.Value >= 3 And el.Value <= 5 _
            Select el %>
    </Root>

Console.WriteLine(xmlTree2)

この例を実行すると、次の出力が生成されます。

<Root>
  <Child3>3</Child3>
  <Child4>4</Child4>
  <Child5>5</Child5>
</Root>

次の例では、さまざまな種類のコンテンツを含む XML ツリーを作成します。

XElement root;

// String content:
root = new XElement("Root", "Some text");
Console.WriteLine(root);

// XElement object content:
root = new XElement("Root",
    new XElement("NewChild", "n")
);
Console.WriteLine(root);

// XAttribute object content:
root = new XElement("Root",
    new XAttribute("NewAttribute", "n")
);
Console.WriteLine(root);

// Double content:
double dbl = 12.345;
root = new XElement("Root", dbl);
Console.WriteLine(root);

// DateTime content:
DateTime dt = new DateTime(2006, 10, 6, 12, 30, 00);
root = new XElement("Root", dt);
Console.WriteLine(root);

// String array content:
// Any collection other than a collection of XElement or XAttribute objects
// are converted to strings. The strings are concatenated and added.
string[] stringArray = {
    "abc",
    "def",
    "ghi"
};
root = new XElement("Root", stringArray);
Console.WriteLine(root);

// XElement object array content:
XElement[] ellArray = {
    new XElement("NewChild1", 1),
    new XElement("NewChild2", 2),
    new XElement("NewChild3", 3)
};
root = new XElement("Root", ellArray);
Console.WriteLine(root);

// XAttribute object array content:
XAttribute[] attArray = {
    new XAttribute("NewAtt1", 1),
    new XAttribute("NewAtt2", 2),
    new XAttribute("NewAtt3", 3)
};
root = new XElement("Root", attArray);
Console.WriteLine(root);
Dim root As XElement

' String content:
root = <Root>Some text</Root>
Console.WriteLine(root)

' XElement object content:
root = <Root>
           <NewChild>n</NewChild>
       </Root>
Console.WriteLine(root)

' XAttribute object content:
root = <Root NewAttribute="n"/>
Console.WriteLine(root)

' Double content:
Dim dbl As Double = 12.345
root = <Root><%= dbl %></Root>
Console.WriteLine(root)

' DateTime content:
Dim dt As DateTime = New DateTime(2006, 10, 6, 12, 30, 0)
root = <Root><%= dt %></Root>
Console.WriteLine(root)

' String array content:
' Any collection other than a collection of XElement or XAttribute objects
' are converted to strings. The strings are concatenated and added.

Dim stringArray As String() = { _
    "abc", _
    "def", _
    "ghi" _
}
root = <Root><%= stringArray %></Root>
Console.WriteLine(root)

' XElement object array content:
Dim ellArray As XElement() = { _
    <NewChild1>1</NewChild1>, _
    <NewChild2>2</NewChild2>, _
    <NewChild3>3</NewChild3> _
}

root = <Root><%= ellArray %></Root>
Console.WriteLine(root)

' XAttribute object array content
Dim attArray As XAttribute() = { _
    New XAttribute("NewAtt1", 1), _
    New XAttribute("NewAtt2", 2), _
    New XAttribute("NewAtt3", 3) _
}
root = <Root><%= attArray %></Root>
Console.WriteLine(root)

この例を実行すると、次の出力が生成されます。

<Root>Some text</Root>
<Root>
  <NewChild>n</NewChild>
</Root>
<Root NewAttribute="n" />
<Root>12.345</Root>
<Root>2006-10-06T12:30:00</Root>
<Root>abcdefghi</Root>
<Root>
  <NewChild1>1</NewChild1>
  <NewChild2>2</NewChild2>
  <NewChild3>3</NewChild3>
</Root>
<Root NewAtt1="1" NewAtt2="2" NewAtt3="3" />

次の例では、名前空間に XML ツリーを作成します。

// Create an XML tree in a namespace.
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
    new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);
' Create an XML tree in a namespace.
Dim root As XElement = _
    <Root xmlns='http://www.adventure-works.com'>
        <Child>child content</Child>
    </Root>
Console.WriteLine(root)

この例を実行すると、次の出力が生成されます。

<Root xmlns="http://www.adventure-works.com">
  <Child>child content</Child>
</Root>

次の例では、入れ子になった名前空間を含む XML ツリーを作成します。

// Create an XML tree with nested namespaces.
XNamespace aw = "http://www.adventure-works.com";
XNamespace fc = "www.fourthcoffee.com";
XElement root = new XElement(aw + "Root",
    new XElement(fc + "Child",
        new XElement(aw + "DifferentChild", "other content")
    )
);
Console.WriteLine(root);
' Create an XML tree with nested namespaces.
Dim root As XDocument = _
    <?xml version='1.0'?>
    <Root xmlns='http://www.adventure-works.com'>
        <Child xmlns='www.fourthcoffee.com'>
        <DifferentChild xmlns='http://www.adventure-works.com'>other content</DifferentChild>
        </Child>
    </Root>
Console.WriteLine(root)

この例を実行すると、次の出力が生成されます。

<Root xmlns="http://www.adventure-works.com">
  <Child xmlns="www.fourthcoffee.com">
    <DifferentChild xmlns="http://www.adventure-works.com">other content</DifferentChild>
  </Child>
</Root>

注釈

このコンストラクターは、指定されたコンテンツと属性を持つ要素を作成します。

文字列から XNameへの暗黙的な変換があります。 このコンストラクターの一般的な用途は、新しい XNameを作成するのではなく、パラメーターとして文字列を指定することです。

名前空間に要素を作成する場合、一般的な用途は、 XNamespace と文字列を使用して加算演算子オーバーロードを使用して XNameを作成することです。 詳細については、「 XML 名前空間の操作」を参照してください。

このコンストラクターに渡すことができる有効なコンテンツの詳細については、「 XElement オブジェクトと XDocument オブジェクトの有効なコンテンツ」を参照してください。

こちらもご覧ください

適用対象