XPathNavigator.Select Yöntem

Tanım

Belirtilen XPath ifadesini kullanarak bir düğüm kümesi seçer.Selects a node set, using the specified XPath expression.

Aşırı Yüklemeler

Select(String)

Belirtilen XPath ifadesini kullanarak bir düğüm kümesi seçer.Selects a node set, using the specified XPath expression.

Select(XPathExpression)

Belirtilen ' i kullanarak bir düğüm kümesi seçer XPathExpression .Selects a node set using the specified XPathExpression.

Select(String, IXmlNamespaceResolver)

IXmlNamespaceResolverAd alanı öneklerini çözümlemek için belirtilen nesneyle belirtilen XPath ifadesini kullanarak bir düğüm kümesi seçer.Selects a node set using the specified XPath expression with the IXmlNamespaceResolver object specified to resolve namespace prefixes.

Select(String)

Belirtilen XPath ifadesini kullanarak bir düğüm kümesi seçer.Selects a node set, using the specified XPath expression.

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

Bir String XPath ifadesini temsil eden.A String representing an XPath expression.

Döndürülenler

XPathNodeIterator

XPathNodeIteratorSeçili düğüm kümesine işaret ediyor.An XPathNodeIterator pointing to the selected node set.

Özel durumlar

XPath ifadesi bir hata içeriyor veya dönüş türü bir düğüm kümesi değil.The XPath expression contains an error or its return type is not a node set.

XPath ifadesi geçerli değil.The XPath expression is not valid.

Örnekler

Aşağıdaki örnek, Select bir düğüm kümesi seçmek için yöntemini kullanır.The following example uses the Select method to select a node set.

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, books.xml dosyayı bir giriş olarak alır.The example takes the books.xml file as an input.

<?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çim bağlamı, XPathNavigator Bu yöntem çağrıldığında öğesinin konumudur.The context for the selection is the position of the XPathNavigator when this method is called. Bu yöntem çağrıldıktan sonra, XPathNodeIterator döndürülen seçili düğümlerin kümesini temsil eder.After calling this method, the XPathNodeIterator returned represents the set of selected nodes. MoveNext XPathNodeIterator Seçili düğüm kümesini yinelemek için ' ın metodunu kullanın.Use MoveNext method of the XPathNodeIterator to iterate over the selected node set.

Aşağıdaki C# kodu seçili düğüm kümesinin üzerinde yinelenir.The following C# code iterates over the selected set of nodes.

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

Yöntemi kullanırken göz önünde bulundurmanız gereken önemli notlar aşağıda verilmiştir Select .The following are important notes to consider when using the Select method.

Ayrıca bkz.

Şunlara uygulanır

Select(XPathExpression)

Belirtilen ' i kullanarak bir düğüm kümesi seçer XPathExpression .Selects a node set using the specified XPathExpression.

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

XPathExpressionDerlenen XPath sorgusunu içeren nesne.An XPathExpression object containing the compiled XPath query.

Döndürülenler

XPathNodeIterator

XPathNodeIteratorSeçili düğüm kümesine işaret eden bir.An XPathNodeIterator that points to the selected node set.

Özel durumlar

XPath ifadesi bir hata içeriyor veya dönüş türü bir düğüm kümesi değil.The XPath expression contains an error or its return type is not a node set.

XPath ifadesi geçerli değil.The XPath expression is not valid.

Örnekler

Aşağıdaki örnek, Select bir düğüm kümesi seçmek için yöntemini kullanır.The following example uses the Select method to select a node set.

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, books.xml dosyayı bir giriş olarak alır.The example takes the books.xml file as an input.

<?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çim bağlamı, XPathNavigator Bu yöntemi çağırdığınızda konumunun konumudur.The context for the selection is the position of the XPathNavigator when you called this method. Bu yöntemi çağırdıktan sonra, XPathNodeIterator döndürülen seçili düğümlerin kümesini temsil eder.After you call this method, the XPathNodeIterator returned represents the set of selected nodes. MoveNext XPathNodeIterator Seçili düğüm kümesini yinelemek için üzerinde kullanın.Use MoveNext on the XPathNodeIterator to iterate over the selected node set.

Aşağıdaki C# kodu seçili düğüm kümesinin üzerinde yinelenir.The following C# code iterates over the selected set of nodes.

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

Yöntemi kullanırken göz önünde bulundurmanız gereken önemli notlar aşağıda verilmiştir Select .The following are important notes to consider when using the Select method.

Örneğin, belgenin aşağıdaki XML düğümlerini içerdiğini varsayalım.For example, suppose the document contains the following XML nodes.

<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 bk:ISBN düğümünü seçer.In this case, the following C# code selects the bk:ISBN node.

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

XPathExpressionBir ön ek içermiyorsa, ad alanı URI 'sinin boş ad alanı olduğu varsayılır.If the XPathExpression does not include a prefix, it is assumed that the namespace URI is the empty namespace. XML 'niz bir varsayılan ad alanı içeriyorsa, yine de SetContext yöntemini kullanmanız ve XmlNamespaceManager varsayılan ad alanını işlemek için bir ön ek ve ad alanı URI 'si içeren bir belirtmeniz gerekir.If your XML includes a default namespace, you must still use the SetContext method and provide an XmlNamespaceManager that contains a prefix and namespace URI to handle the default namespace.

Örneğin, aşağıdaki XML 'e sahip olduğunuzu varsayalım.For example, suppose you have the following XML.

<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:In this case, the following C# code selects all book nodes:

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 üzerinde hiçbir etkisi yoktur XPathNavigator .This method has no effect on the state of the XPathNavigator.

Ayrıca bkz.

Şunlara uygulanır

Select(String, IXmlNamespaceResolver)

IXmlNamespaceResolverAd alanı öneklerini çözümlemek için belirtilen nesneyle belirtilen XPath ifadesini kullanarak bir düğüm kümesi seçer.Selects a node set using the specified XPath expression with the IXmlNamespaceResolver object specified to resolve namespace prefixes.

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

Bir String XPath ifadesini temsil eden.A String representing an XPath expression.

resolver
IXmlNamespaceResolver

IXmlNamespaceResolverAd alanı öneklerini çözümlemek için kullanılan nesne.The IXmlNamespaceResolver object used to resolve namespace prefixes.

Döndürülenler

XPathNodeIterator

XPathNodeIteratorSeçili düğüm kümesine işaret eden bir.An XPathNodeIterator that points to the selected node set.

Özel durumlar

XPath ifadesi bir hata içeriyor veya dönüş türü bir düğüm kümesi değil.The XPath expression contains an error or its return type is not a node set.

XPath ifadesi geçerli değil.The XPath expression is not valid.

Örnekler

Aşağıdaki örnek, Select XmlNamespaceManager XPath ifadesinde ad alanı öneklerini çözümlemek için belirtilen nesneyle yöntemi kullanılarak bir düğüm kümesi seçmeyi göstermektedir.The following example illustrates selecting a node set using the Select method with the XmlNamespaceManager object specified to resolve namespace prefixes in the XPath expression.

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, contosoBooks.xml dosyayı bir giriş olarak alır.The example takes the contosoBooks.xml file as an input.

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