XContainer.Element(XName) Método
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Obtém o primeiro elemento filho (em ordem de documento) com o 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
Retornos
Um XElement que corresponde ao especificado XNameou null.
Exemplos
O exemplo a seguir mostra dois usos desse método. Em um caso, o método localiza o elemento em srcTree. No segundo caso, o método não encontra o elemento na árvore de origem, nenhum elemento é adicionado xmlTreee nenhuma exceção é gerada.
Observe que o exemplo Visual Basic usa a propriedade XML filho. Também é permitido usar o método Element diretamente no 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 a seguir é o mesmo, mas nesse caso o XML está em um namespace. Para obter 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>
Comentários
Retorna null se não houver nenhum elemento com o nome especificado.
Alguns métodos de eixo retornam coleções de elementos ou atributos. Esse método retorna apenas um único elemento.
Esse método retornará null se o elemento com o nome especificado não for encontrado. Todos os métodos que permitem construir elementos (o construtor de XElement, Adde assim por diante) aceitam null como um argumento válido. Isso permite que você use um idioma conveniente: você pode chamar esse método como parte da construção funcional e o elemento é adicionado à árvore XML que está sendo construída se e somente se o elemento existir na árvore de origem. O exemplo a seguir mostra esse idioma.
Ao contrário de Elements, esse método não é um método de eixo. Ele não usa a execução adiada; ele simplesmente retorna um elemento quando chamado.