XPathNavigator.Select Yöntem

Tanım

Belirtilen XPath ifadesini kullanarak bir düğüm kümesi seçer.

Aşırı Yüklemeler

Name Description
Select(String)

Belirtilen XPath ifadesini kullanarak bir düğüm kümesi seçer.

Select(XPathExpression)

Belirtilen XPathExpressionöğesini kullanarak bir düğüm kümesi seçer.

Select(String, IXmlNamespaceResolver)

Ad alanı ön eklerini çözümlemek için belirtilen nesneyle belirtilen IXmlNamespaceResolver XPath ifadesini kullanarak bir düğüm kümesi seçer.

Select(String)

Kaynak:
XPathNavigator.cs
Kaynak:
XPathNavigator.cs
Kaynak:
XPathNavigator.cs
Kaynak:
XPathNavigator.cs
Kaynak:
XPathNavigator.cs

Belirtilen XPath ifadesini kullanarak bir düğüm kümesi seçer.

public:
 virtual System::Xml::XPath::XPathNodeIterator ^ Select(System::String ^ xpath);
public virtual System.Xml.XPath.XPathNodeIterator Select(string xpath);
abstract member Select : string -> System.Xml.XPath.XPathNodeIterator
override this.Select : string -> System.Xml.XPath.XPathNodeIterator
Public Overridable Function Select (xpath As String) As XPathNodeIterator

Parametreler

xpath
String

String XPath ifadesini temsil eden.

Döndürülenler

XPathNodeIterator Seçili düğüm kümesine işaret eden bir.

Özel durumlar

XPath ifadesi bir hata içeriyor veya dönüş türü bir düğüm kümesi değil.

XPath ifadesi geçerli değil.

Örnekler

Aşağıdaki örnek, bir düğüm kümesi seçmek için yöntemini kullanır Select .

XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

XPathNodeIterator nodes = navigator.Select("/bookstore/book");
nodes.MoveNext();
XPathNavigator nodesNavigator = nodes.Current;

XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Text, false);

while (nodesText.MoveNext())
    Console.WriteLine(nodesText.Current.Value);
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim nodes As XPathNodeIterator = navigator.Select("/bookstore/book")
nodes.MoveNext()
Dim nodesNavigator As XPathNavigator = nodes.Current

Dim nodesText As XPathNodeIterator = nodesNavigator.SelectDescendants(XPathNodeType.Text, False)

While nodesText.MoveNext()
    Console.WriteLine(nodesText.Current.Value)
End While

Örnek, books.xml dosyasını giriş olarak alır.

<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
        <author>
            <first-name>Herman</first-name>
            <last-name>Melville</last-name>
        </author>
        <price>11.99</price>
    </book>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
            <name>Plato</name>
        </author>
        <price>9.99</price>
    </book>
</bookstore>

Açıklamalar

Seçimin bağlamı, bu yöntemin XPathNavigator çağrıldığı konumudur. Bu yöntem çağrıldıktan sonra döndürülen, XPathNodeIterator seçilen düğüm kümesini temsil eder. Seçili düğüm kümesini yinelemek için yöntemini MoveNext kullanınXPathNodeIterator.

Aşağıdaki C# kodu seçilen düğüm kümesi üzerinde yinelenir.

XPathNodeIterator iterator = nav.Select("/bookstore/book");
while (iterator.MoveNext())
{
    Console.WriteLine(Iterator.Current.Name);
}

Yöntemi kullanırken dikkate alınması gereken önemli notlar aşağıdadır Select .

Ayrıca bkz.

Şunlara uygulanır

Select(XPathExpression)

Kaynak:
XPathNavigator.cs
Kaynak:
XPathNavigator.cs
Kaynak:
XPathNavigator.cs
Kaynak:
XPathNavigator.cs
Kaynak:
XPathNavigator.cs

Belirtilen XPathExpressionöğesini kullanarak bir düğüm kümesi seçer.

public:
 virtual System::Xml::XPath::XPathNodeIterator ^ Select(System::Xml::XPath::XPathExpression ^ expr);
public virtual System.Xml.XPath.XPathNodeIterator Select(System.Xml.XPath.XPathExpression expr);
abstract member Select : System.Xml.XPath.XPathExpression -> System.Xml.XPath.XPathNodeIterator
override this.Select : System.Xml.XPath.XPathExpression -> System.Xml.XPath.XPathNodeIterator
Public Overridable Function Select (expr As XPathExpression) As XPathNodeIterator

Parametreler

expr
XPathExpression

XPathExpression Derlenmiş XPath sorgusunu içeren bir nesne.

Döndürülenler

XPathNodeIterator Seçili düğüm kümesine işaret eden bir.

Özel durumlar

XPath ifadesi bir hata içeriyor veya dönüş türü bir düğüm kümesi değil.

XPath ifadesi geçerli değil.

Örnekler

Aşağıdaki örnek, bir düğüm kümesi seçmek için yöntemini kullanır Select .

XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

XPathExpression query = navigator.Compile("/bookstore/book");
XPathNodeIterator nodes = navigator.Select(query);
XPathNavigator nodesNavigator = nodes.Current;

XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Text, false);

while (nodesText.MoveNext())
{
    Console.WriteLine(nodesText.Current.Value);
}
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim query As XPathExpression = navigator.Compile("/bookstore/book")
Dim nodes As XPathNodeIterator = navigator.Select(query)
Dim nodesNavigator As XPathNavigator = nodes.Current

Dim nodesText As XPathNodeIterator = nodesNavigator.SelectDescendants(XPathNodeType.Text, False)

While nodesText.MoveNext()
    Console.WriteLine(nodesText.Current.Value)
End While

Örnek, books.xml dosyasını giriş olarak alır.

<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
        <author>
            <first-name>Herman</first-name>
            <last-name>Melville</last-name>
        </author>
        <price>11.99</price>
    </book>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
            <name>Plato</name>
        </author>
        <price>9.99</price>
    </book>
</bookstore>

Açıklamalar

Seçimin bağlamı, bu yöntemi çağırdığınızda öğesinin XPathNavigator konumudur. Bu yöntemi çağırdıktan sonra döndürülen, XPathNodeIterator seçili düğüm kümesini temsil eder. Seçili düğüm kümesini yinelemek için üzerinde MoveNext kullanınXPathNodeIterator.

Aşağıdaki C# kodu seçilen düğüm kümesi üzerinde yinelenir.

XPathNodeIterator ni = nav.Select(expr);
while (ni.MoveNext())
{
    Console.WriteLine(ni.Current.Name);
}

Yöntemi kullanırken dikkate alınması gereken önemli notlar aşağıdadır Select .

Örneğin, belgenin aşağıdaki XML düğümlerini içerdiğini varsayalım.

<bookstore xmlns:bk='urn:samples'>
    <book bk:ISBN='1-325-0980'>
        <title>Pride And Prejudice</title>
    </book>
</bookstore>

Bu durumda, aşağıdaki C# kodu düğümü seçer bk:ISBN .

XPathExpression expr = nav.Compile("book/@bk:ISBN");
XmlNamespaceManager mngr = new XmlNamespaceManager(new NameTable());
mngr.AddNamespace("bk","urn:samples");
expr.SetContext(mngr);
XPathNodeIterator ni = nav.Select(expr);

Note

XPathExpression bir ön ek içermiyorsa, ad alanı URI'sinin boş ad alanı olduğu varsayılır. XML'niz varsayılan bir ad alanı içeriyorsa, yine yöntemini kullanmanız SetContext ve varsayılan ad alanını işlemek için ön ek ve ad alanı URI'sini içeren bir XmlNamespaceManager sağlamanız gerekir.

Örneğin, aşağıdaki XML'e sahip olduğunuzu varsayalım.

<bookstore xmlns="http://www.lucernepublishing.com">
    <book>
        <title>Pride And Prejudice</title>
    </book>
</bookstore>

Bu durumda, aşağıdaki C# kodu tüm kitap düğümlerini seçer:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XPathExpression expr;
expr = nav.Compile("//ab:book");
expr.SetContext(nsmgr);
XPathNodeIterator ni = nav.Select(expr);

Bu yöntemin durumu XPathNavigatorüzerinde hiçbir etkisi yoktur.

Ayrıca bkz.

Şunlara uygulanır

Select(String, IXmlNamespaceResolver)

Kaynak:
XPathNavigator.cs
Kaynak:
XPathNavigator.cs
Kaynak:
XPathNavigator.cs
Kaynak:
XPathNavigator.cs
Kaynak:
XPathNavigator.cs

Ad alanı ön eklerini çözümlemek için belirtilen nesneyle belirtilen IXmlNamespaceResolver XPath ifadesini kullanarak bir düğüm kümesi seçer.

public:
 virtual System::Xml::XPath::XPathNodeIterator ^ Select(System::String ^ xpath, System::Xml::IXmlNamespaceResolver ^ resolver);
public virtual System.Xml.XPath.XPathNodeIterator Select(string xpath, System.Xml.IXmlNamespaceResolver? resolver);
public virtual System.Xml.XPath.XPathNodeIterator Select(string xpath, System.Xml.IXmlNamespaceResolver resolver);
abstract member Select : string * System.Xml.IXmlNamespaceResolver -> System.Xml.XPath.XPathNodeIterator
override this.Select : string * System.Xml.IXmlNamespaceResolver -> System.Xml.XPath.XPathNodeIterator
Public Overridable Function Select (xpath As String, resolver As IXmlNamespaceResolver) As XPathNodeIterator

Parametreler

xpath
String

String XPath ifadesini temsil eden.

resolver
IXmlNamespaceResolver

IXmlNamespaceResolver Ad alanı ön eklerini çözümlemek için kullanılan nesne.

Döndürülenler

XPathNodeIterator Seçili düğüm kümesine işaret eden bir.

Özel durumlar

XPath ifadesi bir hata içeriyor veya dönüş türü bir düğüm kümesi değil.

XPath ifadesi geçerli değil.

Örnekler

Aşağıdaki örnekte, XPath ifadesindeki Select ad alanı ön eklerini çözümlemek için belirtilen nesneyle XmlNamespaceManager yöntemi kullanılarak bir düğüm kümesinin seçilmesi gösterilmektedir.

XPathDocument document = new XPathDocument("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();
XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("bk", "http://www.contoso.com/books");

XPathNodeIterator nodes = navigator.Select("/bk:bookstore/bk:book/bk:price", manager);
// Move to the first node bk:price node
if(nodes.MoveNext())
{
    // now nodes.Current points to the first selected node
    XPathNavigator nodesNavigator = nodes.Current;

    //select all the descendants of the current price node
    XPathNodeIterator nodesText =
       nodesNavigator.SelectDescendants(XPathNodeType.Text, false);

    while(nodesText.MoveNext())
    {
       Console.WriteLine(nodesText.Current.Value);
    }
}
Dim document As XPathDocument = New XPathDocument("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim manager As XmlNamespaceManager = New XmlNamespaceManager(navigator.NameTable)
manager.AddNamespace("bk", "http://www.contoso.com/books")

Dim nodes As XPathNodeIterator = navigator.Select("/bk:bookstore/bk:book/bk:price", manager)
' Move to the first node bk:price node.
If (nodes.MoveNext()) Then
    ' Now nodes.Current points to the first selected node.
    Dim nodesNavigator As XPathNavigator = nodes.Current

    ' Select all the descendants of the current price node.
    Dim nodesText As XPathNodeIterator = nodesNavigator.SelectDescendants(XPathNodeType.Text, False)

    While nodesText.MoveNext()
        Console.WriteLine(nodesText.Current.Value)
    End While
End If

Örnek, contosoBooks.xml dosyasını giriş olarak alır.

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns="http://www.contoso.com/books">
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
        <author>
            <first-name>Herman</first-name>
            <last-name>Melville</last-name>
        </author>
        <price>11.99</price>
    </book>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
            <name>Plato</name>
        </author>
        <price>9.99</price>
    </book>
</bookstore>

Şunlara uygulanır