XmlNode.SelectNodes 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
選取與 XPath 表達式相符的節點清單。
多載
| 名稱 | Description |
|---|---|
| SelectNodes(String) |
選取與 XPath 表達式相符的節點清單。 |
| SelectNodes(String, XmlNamespaceManager) |
選取與 XPath 表達式相符的節點清單。 XPath 表達式中發現的任何前綴皆可透過提供的 XmlNamespaceManager解析 。 |
備註
XPath 表達式可以包含命名空間。 命名空間解析支援使用 XmlNamespaceManager。 若 XPath 表達式包含前綴,則必須將前綴與命名空間 URI 對加入 XmlNamespaceManager。
備註
若 XPath 表達式未包含前綴,則假設命名空間 URI 為空命名空間。 如果你的 XML 包含預設命名空間,你仍然必須在 中 XmlNamespaceManager加入前綴和命名空間 URI;否則,你將無法被選取任何節點。
如需詳細資訊,請參閱 使用 XPath 瀏覽選取節點。 針對程式碼範例,請從前一節的過載清單中選擇一個過載。
SelectNodes(String)
- 來源:
- XmlNode.cs
- 來源:
- XmlNode.cs
- 來源:
- XmlNode.cs
- 來源:
- XmlNode.cs
- 來源:
- XmlNode.cs
選取與 XPath 表達式相符的節點清單。
public:
System::Xml::XmlNodeList ^ SelectNodes(System::String ^ xpath);
public System.Xml.XmlNodeList? SelectNodes(string xpath);
public System.Xml.XmlNodeList SelectNodes(string xpath);
member this.SelectNodes : string -> System.Xml.XmlNodeList
Public Function SelectNodes (xpath As String) As XmlNodeList
參數
- xpath
- String
XPath 的表達方式。
傳回
an XmlNodeList 包含一組與 XPath 查詢相符的節點。
例外狀況
XPath 表達式包含一個前綴。
範例
以下範例選取所有作者姓氏為奧斯汀的書籍,並更改這些書籍的價格。
using System;
using System.Xml;
public class Sample6
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml");
XmlNodeList nodeList;
XmlNode root = doc.DocumentElement;
nodeList = root.SelectNodes("descendant::book[author/last-name='Austen']");
//Change the price on the books.
foreach (XmlNode book in nodeList)
{
book.LastChild.InnerText = "15.95";
}
Console.WriteLine("Display the modified XML document....");
doc.Save(Console.Out);
}
}
Imports System.IO
Imports System.Xml
public class Sample
public shared sub Main()
'Create the XmlDocument.
Dim doc as XmlDocument = new XmlDocument()
doc.Load("booksort.xml")
Dim book as XmlNode
Dim nodeList as XmlNodeList
Dim root as XmlNode = doc.DocumentElement
nodeList=root.SelectNodes("descendant::book[author/last-name='Austen']")
'Change the price on the books.
for each book in nodeList
book.LastChild.InnerText="15.95"
next
Console.WriteLine("Display the modified XML document....")
doc.Save(Console.Out)
end sub
end class
此範例使用以下 XML:
<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>
備註
如果 XPath 表達式需要命名空間解析,你必須使用 SelectNodes 以 為 XmlNamespaceManager 參數的超載。 用來 XmlNamespaceManager 解析命名空間。
備註
若 XPath 表達式未包含前綴,則假設命名空間 URI 為空命名空間。 如果你的 XML 包含預設命名空間,你仍必須使用 並 XmlNamespaceManager 新增前綴和命名空間 URI;否則,你將無法被選取任何節點。 如需詳細資訊,請參閱 使用 XPath 瀏覽選取節點。
備註
在表達式中,一個常見的問題是如何在表達式中加入單引號(')或雙引號(“)。 如果你需要搜尋包含單一引號的數值,必須用雙引號包住字串。 如果你需要搜尋包含雙引號的數值,必須用單引號包住字串。
例如,假設您有下列 XML:
<bookstore>
<book>
<title>'Emma'</title>
</book>
</bookstore>
以下 Visual Basic 程式碼選擇包含單引號的元素:
nodeList = root.SelectNodes("//book[contains(title,""'Emma'"")]")
此方法是 Microsoft 對文件物件模型(DOM)的擴充。
XmlNodeList此方法回傳的物件在底層文件不變時仍有效。 若底層文件變更,可能會回傳意外結果(不會拋出例外)。
另請參閱
適用於
SelectNodes(String, XmlNamespaceManager)
- 來源:
- XmlNode.cs
- 來源:
- XmlNode.cs
- 來源:
- XmlNode.cs
- 來源:
- XmlNode.cs
- 來源:
- XmlNode.cs
選取與 XPath 表達式相符的節點清單。 XPath 表達式中發現的任何前綴皆可透過提供的 XmlNamespaceManager解析 。
public:
System::Xml::XmlNodeList ^ SelectNodes(System::String ^ xpath, System::Xml::XmlNamespaceManager ^ nsmgr);
public System.Xml.XmlNodeList? SelectNodes(string xpath, System.Xml.XmlNamespaceManager nsmgr);
public System.Xml.XmlNodeList SelectNodes(string xpath, System.Xml.XmlNamespaceManager nsmgr);
member this.SelectNodes : string * System.Xml.XmlNamespaceManager -> System.Xml.XmlNodeList
Public Function SelectNodes (xpath As String, nsmgr As XmlNamespaceManager) As XmlNodeList
參數
- xpath
- String
XPath 的表達方式。
- nsmgr
- XmlNamespaceManager
XmlNamespaceManager用於解析 XPath 表達式中前綴的命名空間。
傳回
an XmlNodeList 包含一組與 XPath 查詢相符的節點。
例外狀況
XPath 表達式包含一個未在 XmlNamespaceManager.
範例
以下範例顯示每個 ISBN 屬性的值。 此範例使用一個 XmlElement 物件,該物件繼承自類別 XmlNode 。
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml");
//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:samples");
//Select and display the value of all the ISBN attributes.
XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectNodes("/bookstore/book/@bk:ISBN", nsmgr);
foreach (XmlNode isbn in nodeList){
Console.WriteLine(isbn.Value);
}
}
}
Imports System.IO
Imports System.Xml
public class Sample
public shared sub Main()
Dim doc as XmlDocument = new XmlDocument()
doc.Load("booksort.xml")
'Create an XmlNamespaceManager for resolving namespaces.
Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("bk", "urn:samples")
'Select and display the value of all the ISBN attributes.
Dim nodeList as XmlNodeList
Dim root as XmlElement = doc.DocumentElement
nodeList = root.SelectNodes("/bookstore/book/@bk:ISBN", nsmgr)
Dim isbn as XmlNode
for each isbn in nodeList
Console.WriteLine(isbn.Value)
next
end sub
end class
範例中使用檔案 booksort.xml,作為輸入。
<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>
備註
XPath 表達式可以包含命名空間。 命名空間解析支援使用 XmlNamespaceManager。 若 XPath 表達式包含前綴,則必須將前綴與命名空間 URI 對加入 XmlNamespaceManager。
備註
若 XPath 表達式未包含前綴,則假設命名空間 URI 為空命名空間。 如果你的 XML 包含預設命名空間,你仍然必須在 中 XmlNamespaceManager加入前綴和命名空間 URI;否則,你將無法被選取任何節點。 如需詳細資訊,請參閱 使用 XPath 瀏覽選取節點。
例如,如果你有以下 XML:
<bookstore xmlns="http://www.lucernepublishing.com">
<book>
<title>Pride And Prejudice</title>
</book>
</bookstore>
以下 C# 程式碼選擇所有書籍節點:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XmlNodeList nodelist = doc.SelectNodes("//ab:book", nsmgr);
備註
在表達式中,一個常見的問題是如何在表達式中加入單引號(')或雙引號(“)。 如果你需要搜尋包含單一引號的數值,必須用雙引號包住字串。 如果你需要搜尋包含雙引號的數值,必須用單引號包住字串。
例如,假設您有下列 XML:
<bookstore xmlns="http://www.lucernepublishing.com">
<book>
<title>'Emma'</title>
</book>
</bookstore>
以下 Visual Basic 程式碼選擇包含單引號的元素:
Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com")
nodeList = root.SelectNodes("//ab:book[contains(ab:title,""'Emma'"")]", nsmgr)
此方法是 Microsoft 對文件物件模型(DOM)的擴充。
XmlNodeList此方法回傳的物件在底層文件不變時仍有效。 若底層文件變更,可能會回傳意外結果(不會拋出例外)。