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과 일치하는 XName이거나 null입니다.

예제

다음 예제에서는 이 메서드의 두 가지 용도를 보여 있습니다. 한 경우 메서드는 .에서 요소를 찾습니다 srcTree. 두 번째 경우 메서드는 소스 트리에서 요소를 찾지 못하고, 요소가 추가 xmlTree되지 않으며, 예외가 throw되지 않습니다.

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 합니다.

일부 축 메서드는 요소 또는 특성의 컬렉션을 반환합니다. 이 메서드는 단일 요소만 반환합니다.

이 메서드는 지정된 이름의 요소를 찾을 수 없는 경우 반환 null 합니다. 요소(생성자 XElementAdd등)를 생성할 수 있는 모든 메서드는 유효한 인수로 허용 null 합니다. 이렇게 하면 편리한 관용구를 사용할 수 있습니다. 함수 생성의 일부로 이 메서드를 호출할 수 있으며 요소가 원본 트리에 있는 경우에만 생성되는 XML 트리에 추가됩니다. 다음 예제에서는 이 관용구를 보여줍니다.

반면 Elements, 이 메서드는 축 메서드가 아닙니다. 지연된 실행은 사용하지 않습니다. 호출할 때 단순히 요소를 반환합니다.

적용 대상

추가 정보