XContainer.Element(XName) Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Obtém o primeiro elemento filho (na ordem do documento) com o elemento especificado 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
Parâmetros
Devoluções
A que corresponde XElement ao especificado XName, ou null.
Exemplos
O exemplo seguinte mostra duas utilizações deste método. Num caso, o método encontra o elemento em srcTree. No segundo caso, o método não encontra o elemento na árvore de origem, nenhum elemento é adicionado a xmlTree, e nenhuma exceção é lançada.
Note que o exemplo do Visual Basic utiliza a propriedade filha XML. Também é permitido usar o método Element diretamente em 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)
Este exemplo produz a seguinte saída:
<Root>
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>
<Child4>4</Child4>
<Child5>5</Child5>
<Element3>3</Element3>
</Root>
O exemplo seguinte é o mesmo, mas neste caso o XML está num namespace. Para mais informações, consulte Trabalhar com Namespaces 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
Este exemplo produz a seguinte saída:
<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>
Observações
Retorna null se não houver elemento com o nome especificado.
Alguns métodos de eixo retornam coleções de elementos ou atributos. Este método retorna apenas um elemento.
Este método retorna null se o elemento com o nome especificado não for encontrado. Todos os métodos que permitem construir elementos (o construtor de XElement, Add, e assim sucessivamente) aceitam null como argumento válido. Isto permite-lhe usar um idioma conveniente: pode chamar este método como parte da construção funcional, e o elemento é adicionado à árvore XML que está a ser construída se e só se o elemento existir na árvore de origem. O exemplo seguinte mostra este idioma.
Em contraste com Elements, este método não é um método de eixo. Não utiliza execução diferida; simplesmente devolve um elemento quando chamado.