Extensions.XPathSelectElement Método

Definição

Seleciona um XElement usando uma expressão XPath.Selects an XElement using a XPath expression.

Sobrecargas

XPathSelectElement(XNode, String)

Seleciona um XElement usando uma expressão XPath.Selects an XElement using a XPath expression.

XPathSelectElement(XNode, String, IXmlNamespaceResolver)

Seleciona um XElement usando uma expressão XPath, resolvendo os prefixos de namespace usando o IXmlNamespaceResolver especificado.Selects an XElement using a XPath expression, resolving namespace prefixes using the specified IXmlNamespaceResolver.

XPathSelectElement(XNode, String)

Seleciona um XElement usando uma expressão XPath.Selects an XElement using a XPath expression.

public:
[System::Runtime::CompilerServices::Extension]
 static System::Xml::Linq::XElement ^ XPathSelectElement(System::Xml::Linq::XNode ^ node, System::String ^ expression);
public static System.Xml.Linq.XElement? XPathSelectElement (this System.Xml.Linq.XNode node, string expression);
public static System.Xml.Linq.XElement XPathSelectElement (this System.Xml.Linq.XNode node, string expression);
static member XPathSelectElement : System.Xml.Linq.XNode * string -> System.Xml.Linq.XElement
<Extension()>
Public Function XPathSelectElement (node As XNode, expression As String) As XElement

Parâmetros

node
XNode

O XNode no qual a expressão XPath será avaliada.The XNode on which to evaluate the XPath expression.

expression
String

Um String que contém uma expressão XPath.A String that contains an XPath expression.

Retornos

XElement

Um XElement ou nulo.An XElement, or null.

Exemplos

O exemplo a seguir cria uma árvore XML pequena e usa XPathSelectElement para selecionar um único elemento.The following example creates a small XML tree and uses XPathSelectElement to select a single element.

                XElement root = new XElement("Root",  
    new XElement("Child1", 1),  
    new XElement("Child2", 2),  
    new XElement("Child3", 3),  
    new XElement("Child4", 4),  
    new XElement("Child5", 5),  
    new XElement("Child6", 6)  
);  
XElement el = root.XPathSelectElement("./Child4");  
Console.WriteLine(el);  
                Dim root As XElement = _  
    <Root>  
        <Child1>1</Child1>  
        <Child2>2</Child2>  
        <Child3>3</Child3>  
        <Child4>4</Child4>  
        <Child5>5</Child5>  
        <Child6>6</Child6>  
    </Root>  
Dim el As XElement = root.XPathSelectElement("./Child4")  
Console.WriteLine(el)  

Esse exemplo gera a saída a seguir:This example produces the following output:

<Child4>4</Child4>  

Aplica-se a

XPathSelectElement(XNode, String, IXmlNamespaceResolver)

Seleciona um XElement usando uma expressão XPath, resolvendo os prefixos de namespace usando o IXmlNamespaceResolver especificado.Selects an XElement using a XPath expression, resolving namespace prefixes using the specified IXmlNamespaceResolver.

public:
[System::Runtime::CompilerServices::Extension]
 static System::Xml::Linq::XElement ^ XPathSelectElement(System::Xml::Linq::XNode ^ node, System::String ^ expression, System::Xml::IXmlNamespaceResolver ^ resolver);
public static System.Xml.Linq.XElement? XPathSelectElement (this System.Xml.Linq.XNode node, string expression, System.Xml.IXmlNamespaceResolver? resolver);
public static System.Xml.Linq.XElement XPathSelectElement (this System.Xml.Linq.XNode node, string expression, System.Xml.IXmlNamespaceResolver resolver);
static member XPathSelectElement : System.Xml.Linq.XNode * string * System.Xml.IXmlNamespaceResolver -> System.Xml.Linq.XElement
<Extension()>
Public Function XPathSelectElement (node As XNode, expression As String, resolver As IXmlNamespaceResolver) As XElement

Parâmetros

node
XNode

O XNode no qual a expressão XPath será avaliada.The XNode on which to evaluate the XPath expression.

expression
String

Um String que contém uma expressão XPath.A String that contains an XPath expression.

resolver
IXmlNamespaceResolver

Um IXmlNamespaceResolver para os prefixos de namespace na expressão XPath.An IXmlNamespaceResolver for the namespace prefixes in the XPath expression.

Retornos

XElement

Um XElement ou nulo.An XElement, or null.

Exemplos

O exemplo a seguir cria uma árvore XML que contém um namespace.The following example creates an XML tree that contains a namespace. Usa XmlReader para ler o documento XML.It uses an XmlReader to read the XML document. Então obtém XmlNameTable de XmlReader, e XmlNamespaceManager de XmlNameTable.It then gets an XmlNameTable from the XmlReader, and an XmlNamespaceManager from the XmlNameTable. Ele usa o XmlNamespaceManager ao selecionar um elemento.It uses the XmlNamespaceManager when selecting an element.

                string markup = @"  
<aw:Root xmlns:aw='http://www.adventure-works.com'>  
    <aw:Child1>child one data</aw:Child1>  
    <aw:Child2>child two data</aw:Child2>  
</aw:Root>";  
XmlReader reader = XmlReader.Create(new StringReader(markup));  
XElement root = XElement.Load(reader);  
XmlNameTable nameTable = reader.NameTable;  
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(nameTable);  
namespaceManager.AddNamespace("aw", "http://www.adventure-works.com");  
XElement child1 = root.XPathSelectElement("./aw:Child1", namespaceManager);  
Console.WriteLine(child1);  
                Dim markup As XElement = _  
    <aw:Root xmlns:aw='http://www.adventure-works.com'>  
        <aw:Child1>child one data</aw:Child1>  
        <aw:Child2>child two data</aw:Child2>  
    </aw:Root>  
Dim reader As XmlReader = markup.CreateReader  
Dim nameTable As XmlNameTable = reader.NameTable  
Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(nameTable)  
namespaceManager.AddNamespace("aw", "http://www.adventure-works.com")  
Dim child1 As XElement = markup.XPathSelectElement("./aw:Child1", namespaceManager)  
Console.WriteLine(child1)  

Esse exemplo gera a saída a seguir:This example produces the following output:

<aw:Child1 xmlns:aw="http://www.adventure-works.com">child one data</aw:Child1>  

Comentários

Você pode usar esse método para avaliar as expressões XPath que contêm prefixos de namespace.You can use this method to evaluate XPath expressions that contain namespace prefixes.

Aplica-se a