XPathNavigator.Select Yöntem

Tanım

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

Aşırı Yüklemeler

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)

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

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 = gcnew 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);
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, dosyayı giriş olarak alır books.xml .

<?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öntem çağrıldığında öğesinin XPathNavigator konumudur. Bu yöntem çağrıldıktan sonra, XPathNodeIterator döndürülen seçilen düğüm kümesini temsil eder. Seçili düğüm kümesinde yineleme yapmak için yöntemini XPathNodeIterator kullanınMoveNext.

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ılırken Select dikkate alınması gereken önemli notlar aşağıdadır.

Ayrıca bkz.

Şunlara uygulanır

Select(XPathExpression)

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

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 = gcnew 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);
}
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, dosyayı giriş olarak alır books.xml .

<?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çilen düğüm kümesini temsil eder. Seçili düğüm kümesini yinelemek için üzerinde XPathNodeIterator kullanınMoveNext.

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ılırken Select dikkate alınması gereken önemli notlar aşağıdadır.

Ö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);  

Not

XPathExpression ö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 de 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)

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

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öntemini kullanarak bir düğüm kümesi seçme gösterilmektedir.

XPathDocument^ document = gcnew XPathDocument("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

XmlNamespaceManager^ manager = gcnew 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);
    }
}
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, dosyayı giriş olarak alır contosoBooks.xml .

<?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