XContainer.Element(XName) Metoda

Definicja

Pobiera pierwszy element podrzędny (w kolejności dokumentu) z określonym XNameelementem .

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

Parametry

name
XName

Element XName do dopasowania.

Zwraca

XElement

Obiekt XElement , który jest zgodny z określonym XNameelementem , lub null.

Przykłady

W poniższym przykładzie przedstawiono dwa zastosowania tej metody. W jednym przypadku metoda znajduje element w elemecie srcTree. W drugim przypadku metoda nie znajduje elementu w drzewie źródłowym, żaden element nie jest dodawany do xmlTreeelementu i nie jest zgłaszany żaden wyjątek.

Należy pamiętać, że w przykładzie Visual Basic użyto podrzędnej właściwości XML. Można również używać Element metody bezpośrednio w 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)  

Ten przykład generuje następujące wyniki:

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

Poniżej przedstawiono ten sam przykład, ale w tym przypadku kod XML znajduje się w przestrzeni nazw. Aby uzyskać więcej informacji, zobacz Praca z przestrzeniami nazw 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  

Ten przykład generuje następujące wyniki:

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

Uwagi

Zwraca wartość null , jeśli nie ma elementu o określonej nazwie.

Niektóre metody osi zwracają kolekcje elementów lub atrybutów. Ta metoda zwraca tylko jeden element.

Ta metoda zwraca wartość null , jeśli element o określonej nazwie nie zostanie znaleziony. Wszystkie metody, które umożliwiają konstruowanie elementów (konstruktora XElement, Additd.) przyjmuje null jako prawidłowy argument. Dzięki temu można użyć wygodnego idiomu: możesz wywołać tę metodę w ramach konstrukcji funkcjonalnej, a element jest dodawany do drzewa XML tworzonego, jeśli i tylko wtedy, gdy element istnieje w drzewie źródłowym. W poniższym przykładzie pokazano ten idiom.

W przeciwieństwie do Elementsmetody ta metoda nie jest metodą osi. Nie używa odroczonego wykonywania; po prostu zwraca element po wywołaniu.

Dotyczy

Zobacz też