XContainer.Element(XName) メソッド

定義

指定した XName の最初の子要素を (ドキュメント順に) 取得します。

public:
 System::Xml::Linq::XElement ^ Element(System::Xml::Linq::XName ^ name);
public System.Xml.Linq.XElement Element (System.Xml.Linq.XName name);
public System.Xml.Linq.XElement? Element (System.Xml.Linq.XName name);
member this.Element : System.Xml.Linq.XName -> System.Xml.Linq.XElement
Public Function Element (name As XName) As XElement

パラメーター

name
XName

照合する XName

戻り値

XElement

指定した XElement に一致するか、nullXName

次の例では、このメソッドの 2 つの使用方法を示します。 1 つのケースでは、メソッド srcTreeは . 2 番目のケースでは、メソッドはソース ツリー内の要素を見つけず、要素は追加 xmlTreeされず、例外はスローされません。

Visual Basicの例では、子 XML プロパティが使用されることに注意してください。 また、Visual BasicでメソッドをElement直接使用することもできます。

XElement srcTree = new XElement("Root",  
    new XElement("Element1", 1),  
    new XElement("Element2", 2),  
    new XElement("Element3", 3),  
    new XElement("Element4", 4),  
    new XElement("Element5", 5)  
);  
XElement xmlTree = new XElement("Root",  
    new XElement("Child1", 1),  
    new XElement("Child2", 2),  
    new XElement("Child3", 3),  
    new XElement("Child4", 4),  
    new XElement("Child5", 5),  
    srcTree.Element("Element3"),  
    // Even though Element9 does not exist in srcTree, the following line  
    // will not throw an exception.  
    srcTree.Element("Element9")  
);  
Console.WriteLine(xmlTree);  
Dim srcTree As XElement = _   
        <Root>  
            <Element1>1</Element1>  
            <Element2>2</Element2>  
            <Element3>3</Element3>  
            <Element4>4</Element4>  
            <Element5>5</Element5>  
        </Root>  

Dim xmlTree As XElement = _  
        <Root>  
            <Child1>1</Child1>  
            <Child2>2</Child2>  
            <Child3>3</Child3>  
            <Child4>4</Child4>  
            <Child5>5</Child5>  
            <%= srcTree.<Element3> %>  
            <%= srcTree.<Element9> %>  
        </Root>  

' Even though Element9 does not exist in srcTree, adding it to the tree  
' will not throw an exception.  

Console.WriteLine(xmlTree)  

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

<Root>  
  <Child1>1</Child1>  
  <Child2>2</Child2>  
  <Child3>3</Child3>  
  <Child4>4</Child4>  
  <Child5>5</Child5>  
  <Element3>3</Element3>  
</Root>  

同じ例を次に示しますが、この場合、XML は名前空間にあります。 詳細については、「 XML 名前空間の操作」を参照してください。

XNamespace aw = "http://www.adventure-works.com";  
XElement srcTree = new XElement(aw + "Root",  
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),  
    new XElement(aw + "Element1", 1),  
    new XElement(aw + "Element2", 2),  
    new XElement(aw + "Element3", 3),  
    new XElement(aw + "Element4", 4),  
    new XElement(aw + "Element5", 5)  
);  
XElement xmlTree = new XElement(aw + "Root",  
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),  
    new XElement(aw + "Child1", 1),  
    new XElement(aw + "Child2", 2),  
    new XElement(aw + "Child3", 3),  
    new XElement(aw + "Child4", 4),  
    new XElement(aw + "Child5", 5),  
    srcTree.Element(aw + "Element3"),  
    // Even though Element9 does not exist in srcTree, the following line  
    // will not throw an exception.  
    srcTree.Element(aw + "Element9")  
);  
Console.WriteLine(xmlTree);  
Imports <xmlns:aw="http://www.adventure-works.com">  

Module Module1  
    Sub Main()  
        Dim srcTree As XElement = _   
            <aw:Root>  
                <aw:Element1>1</aw:Element1>  
                <aw:Element2>2</aw:Element2>  
                <aw:Element3>3</aw:Element3>  
                <aw:Element4>4</aw:Element4>  
                <aw:Element5>5</aw:Element5>  
            </aw:Root>  

        Dim xmlTree As XElement = _  
            <aw:Root>  
                <aw:Child1>1</aw:Child1>  
                <aw:Child2>2</aw:Child2>  
                <aw:Child3>3</aw:Child3>  
                <aw:Child4>4</aw:Child4>  
                <aw:Child5>5</aw:Child5>  
                <%= srcTree.<aw:Element3> %>  
                <%= srcTree.<aw:Element9> %>  
            </aw:Root>  

        ' Even though Element9 does not exist in srcTree, adding it to the tree  
        ' will not throw an exception.  

        Console.WriteLine(xmlTree)  
    End Sub  
End Module  

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

<aw:Root xmlns:aw="http://www.adventure-works.com">  
  <aw:Child1>1</aw:Child1>  
  <aw:Child2>2</aw:Child2>  
  <aw:Child3>3</aw:Child3>  
  <aw:Child4>4</aw:Child4>  
  <aw:Child5>5</aw:Child5>  
  <aw:Element3>3</aw:Element3>  
</aw:Root>  

注釈

指定した名前の null 要素がない場合に返します。

一部の軸メソッドは、要素または属性のコレクションを返します。 このメソッドは、1 つの要素のみを返します。

このメソッドは、 null 指定した名前の要素が見つからない場合に返します。 要素 (、のコンストラクターXElementなど) を構築できるメソッドはすべて、有効な引数として受け入れますnullAdd これにより、便利なイディオムを使用できます。このメソッドは機能構築の一環として呼び出すことができます。要素は、要素がソース ツリーに存在する場合にのみ、構築される XML ツリーに追加されます。 次の例は、このイディオムを示しています。

これに対し Elements、このメソッドは軸メソッドではありません。 遅延実行は使用されません。呼び出されたときに要素を返すだけです。

適用対象

こちらもご覧ください