XContainer.Element(XName) 方法

定义

获取具有指定的 XName 的第一个(按文档顺序)子元素。Gets the first (in document order) child element with the specified XName.

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

要匹配的 XNameThe XName to match.

返回

与指定的 XName 相匹配的 XElement,或者为 nullA XElement that matches the specified XName, or null.

示例

下面的示例演示了此方法的两种用法。The following example shows two uses of this method. 在一种情况下,该方法将查找 srcTree中的元素。In one case, the method finds the element in srcTree. 在第二种情况下,方法在源树中找不到元素,不会将元素添加到 xmlTree中,也不会引发异常。In the second case, the method does not find the element in the source tree, no element is added to xmlTree, and no exception is thrown.

请注意,Visual Basic 示例使用子 XML 属性。Note that the Visual Basic example uses the child XML property. 还允许在 Visual Basic 中直接使用 Element 方法。It is also allowable to use the Element method directly in Visual Basic.

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)  

该示例产生下面的输出:This example produces the following output:

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

下面是同一个示例,但在这种情况下,XML 位于命名空间中。The following is the same example, but in this case the XML is in a namespace. 有关详细信息,请参阅使用XML 命名空间For more information, see Working with XML Namespaces.

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  

该示例产生下面的输出:This example produces the following output:

<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>  

注解

如果不存在具有指定名称的元素,则返回 nullReturns null if there is no element with the specified name.

某些轴方法返回元素或属性的集合。Some axis methods return collections of elements or attributes. 此方法只返回一个元素。This method returns only a single element.

如果找不到具有指定名称的元素,此方法将返回 nullThis method returns null if the element with the specified name is not found. 所有允许构造元素的方法(XElementAdd等的构造函数)接受 null 作为有效自变量。All of the methods that allow you to construct elements (the constructor of XElement, Add, and so on) accept null as a valid argument. 这使您可以使用一个便利的方法:可以调用此方法作为函数构造的一部分,当且仅当该元素存在于源树中时,该元素将添加到正在构造的 XML 树中。This allows you to use a convenient idiom: you can call this method as part of functional construction, and the element is added to the XML tree being constructed if and only if the element exists in the source tree. 下面的示例演示了这种用法。The following example shows this idiom.

Elements相比,此方法不是轴方法。In contrast to Elements, this method is not an axis method. 它不使用延迟的执行;它在被调用时只返回一个元素。It does not use deferred execution; it simply returns an element when called.

适用于

另请参阅