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

符合指定之 XElementXName,或者 null

範例

下列範例顯示此方法的兩個用法。 在一種情況下,方法會在 中 srcTree 尋找 專案。 第二個案例中,方法找不到來源樹狀結構中的專案,不會將任何專案新增至 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如果沒有具有指定名稱的專案,則傳回 。

某些軸方法會傳回專案或屬性的集合。 這個方法只會傳回單一元素。

如果找不到具有指定名稱的專案,這個方法會 null 傳回 。 所有可讓您建構專案的方法 (、 AddXElement 建構函式,依此類) 接受 null 為有效的引數。 這可讓您使用方便的慣用語:您可以呼叫這個方法做為功能建構的一部分,而且只有在來源樹狀結構中有專案存在時,才會將 元素加入至要建構的 XML 樹狀結構。 下列範例顯示這個慣用語。

相較于 Elements ,這個方法不是座標軸方法。 它不會使用延後執行;它只會在呼叫時傳回專案。

適用於

另請參閱