XPathNavigator.Select 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
使用指定的 XPath 表達式選擇節點集合。
多載
| 名稱 | Description |
|---|---|
| Select(String) |
使用指定的 XPath 表達式選擇節點集合。 |
| Select(XPathExpression) |
利用指定的 XPathExpression節點集合選擇。 |
| Select(String, IXmlNamespaceResolver) |
使用指定的 XPath 表達式選擇節點集合,並 IXmlNamespaceResolver 指定物件以解析命名空間前綴。 |
Select(String)
使用指定的 XPath 表達式選擇節點集合。
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
參數
傳回
XPathNodeIterator指向所選節點集合。
例外狀況
XPath 表達式包含錯誤或其回傳類型不是節點集合。
XPath 表達式無效。
範例
以下範例使用該 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
此範例會將 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>
備註
選擇的上下文是該方法被呼叫時的位置 XPathNavigator 。 呼叫此方法後, XPathNodeIterator 返回的代表所選節點的集合。 使用 MoveNext 的方法 XPathNodeIterator 來遍歷所選節點集。
以下 C# 程式碼會遍歷所選節點集合。
XPathNodeIterator iterator = nav.Select("/bookstore/book");
while (iterator.MoveNext())
{
Console.WriteLine(Iterator.Current.Name);
}
以下是使用此 Select 方法時需注意的重要事項。
你仍然可以使用物件的任何 XPathNavigator 導航方法在 XPathNavigator。 XPathNavigator導航方法與所選節點XPathNodeIterator無關。
未來對該 Select 方法的呼叫會回傳一個新的 XPathNodeIterator 物件,指向與新 Select 呼叫相符的節點集合。 這兩個 XPathNodeIterator 物件彼此完全獨立。
如果 XPath 表達式需要命名空間解析,請使用 Select overload,該 以 為 XPathExpression 參數。
此方法對 的 XPathNavigator狀態沒有影響。
另請參閱
- SelectNodes(String)
- SelectSingleNode(String)
- SelectDescendants(XPathNodeType, Boolean)
- SelectChildren(XPathNodeType)
- SelectAncestors(XPathNodeType, Boolean)
適用於
Select(XPathExpression)
利用指定的 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
參數
- expr
- XPathExpression
一個 XPathExpression 包含已編譯 XPath 查詢的物件。
傳回
指向 XPathNodeIterator 所選節點集合的 。
例外狀況
XPath 表達式包含錯誤或其回傳類型不是節點集合。
XPath 表達式無效。
範例
以下範例使用該 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
此範例會將 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>
備註
選擇的上下文是你呼叫此方法時的位置 XPathNavigator 。 呼叫此方法後,return 代表 XPathNodeIterator 所選節點的集合。 在 上 MoveNext 來XPathNodeIterator遍歷所選節點集。
以下 C# 程式碼會遍歷所選節點集合。
XPathNodeIterator ni = nav.Select(expr);
while (ni.MoveNext())
{
Console.WriteLine(ni.Current.Name);
}
以下是使用此 Select 方法時需注意的重要事項。
你仍然可以使用物件的任何 XPathNavigator 導航方法在 XPathNavigator。 XPathNavigator導航方法與所選節點XPathNodeIterator無關。
未來對該 Select 方法的呼叫會回傳一個新的 XPathNodeIterator 物件,指向與新 Select 呼叫相符的節點集合。 這兩個 XPathNodeIterator 物件彼此完全獨立。
若 需要 XPathExpression 命名空間解析,則必須將前綴與命名空間 URI 對加入 XmlNamespaceManager,並 SetContext 呼叫該方法指定 XmlNamespaceManager 來命名空間解析。
例如,假設文件包含以下 XML 節點。
<bookstore xmlns:bk='urn:samples'>
<book bk:ISBN='1-325-0980'>
<title>Pride And Prejudice</title>
</book>
</bookstore>
此時,以下 C# 程式碼選擇節點。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 包含前綴,則假設命名空間 URI 為空命名空間。 如果你的 XML 包含預設命名空間,你仍必須使用該 SetContext 方法,並提供包含前綴和命名空間 URI 的 URI XmlNamespaceManager 來處理預設命名空間。
舉例來說,假設你有以下的 XML。
<bookstore xmlns="http://www.lucernepublishing.com">
<book>
<title>Pride And Prejudice</title>
</book>
</bookstore>
此時,以下 C# 程式碼會選擇所有書籍節點:
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);
此方法對 的 XPathNavigator狀態沒有影響。
另請參閱
- SelectNodes(String)
- SelectSingleNode(String)
- SelectDescendants(XPathNodeType, Boolean)
- SelectChildren(XPathNodeType)
- SelectAncestors(XPathNodeType, Boolean)
適用於
Select(String, IXmlNamespaceResolver)
使用指定的 XPath 表達式選擇節點集合,並 IXmlNamespaceResolver 指定物件以解析命名空間前綴。
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
參數
- resolver
- IXmlNamespaceResolver
用於解析命名空間前綴的 IXmlNamespaceResolver 物件。
傳回
指向 XPathNodeIterator 所選節點集合的 。
例外狀況
XPath 表達式包含錯誤或其回傳類型不是節點集合。
XPath 表達式無效。
範例
以下範例說明如何使用 Select 方法選擇節點集,並指定 XmlNamespaceManager 物件以解析 XPath 表達式中的命名空間前綴。
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
此範例會將 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>