Extensions.XPathSelectElement Extensions.XPathSelectElement Extensions.XPathSelectElement Extensions.XPathSelectElement Method

Definizione

Viene selezionato XElement utilizzando un'espressione XPath.Selects an XElement using a XPath expression.

Overload

XPathSelectElement(XNode, String) XPathSelectElement(XNode, String) XPathSelectElement(XNode, String) XPathSelectElement(XNode, String)

Viene selezionato XElement utilizzando un'espressione XPath.Selects an XElement using a XPath expression.

XPathSelectElement(XNode, String, IXmlNamespaceResolver) XPathSelectElement(XNode, String, IXmlNamespaceResolver) XPathSelectElement(XNode, String, IXmlNamespaceResolver) XPathSelectElement(XNode, String, IXmlNamespaceResolver)

Viene selezionato XElement utilizzando un'espressione XPath e risolvendo i prefissi degli spazi dei nomi tramite l'oggetto IXmlNamespaceResolver specificato.Selects an XElement using a XPath expression, resolving namespace prefixes using the specified IXmlNamespaceResolver.

XPathSelectElement(XNode, String) XPathSelectElement(XNode, String) XPathSelectElement(XNode, String) XPathSelectElement(XNode, String)

Viene selezionato XElement utilizzando un'espressione 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);
static member XPathSelectElement : System.Xml.Linq.XNode * string -> System.Xml.Linq.XElement
<Extension()>
Public Function XPathSelectElement (node As XNode, expression As String) As XElement

Parametri

node
XNode XNode XNode XNode

XNode sul quale valutare l'espressione XPath.The XNode on which to evaluate the XPath expression.

expression
String String String String

String contenente un'espressione XPath.A String that contains an XPath expression.

Restituisce

Esempi

Nell'esempio seguente crea un piccolo albero XML e Usa XPathSelectElement per selezionare un singolo 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)  

Questo esempio produce il seguente output:This example produces the following output:

<Child4>4</Child4>  

XPathSelectElement(XNode, String, IXmlNamespaceResolver) XPathSelectElement(XNode, String, IXmlNamespaceResolver) XPathSelectElement(XNode, String, IXmlNamespaceResolver) XPathSelectElement(XNode, String, IXmlNamespaceResolver)

Viene selezionato XElement utilizzando un'espressione XPath e risolvendo i prefissi degli spazi dei nomi tramite l'oggetto IXmlNamespaceResolver specificato.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);
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

Parametri

node
XNode XNode XNode XNode

XNode sul quale valutare l'espressione XPath.The XNode on which to evaluate the XPath expression.

expression
String String String String

String contenente un'espressione XPath.A String that contains an XPath expression.

resolver
IXmlNamespaceResolver IXmlNamespaceResolver IXmlNamespaceResolver IXmlNamespaceResolver

IXmlNamespaceResolver per i prefissi degli spazi dei nomi nell'espressione XPath.An IXmlNamespaceResolver for the namespace prefixes in the XPath expression.

Restituisce

Esempi

L'esempio seguente crea un albero XML che contiene uno spazio dei nomi.The following example creates an XML tree that contains a namespace. Per leggere il documento XML viene usato XmlReader.It uses an XmlReader to read the XML document. Quindi si ottiene XmlNameTable da XmlReader e XmlNamespaceManager da XmlNameTable.It then gets an XmlNameTable from the XmlReader, and an XmlNamespaceManager from the XmlNameTable. Usa il XmlNamespaceManager quando si seleziona un 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)  

Questo esempio produce il seguente output:This example produces the following output:

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

Commenti

È possibile utilizzare questo metodo per valutare le espressioni XPath che contengono i prefissi dello spazio dei nomi.You can use this method to evaluate XPath expressions that contain namespace prefixes.

Si applica a