选择匹配 XPath 表达式的节点列表。
[VisualBasic] Overloads Public Function SelectNodes( _ ByVal xpath As String _ ) As XmlNodeList [C#] public XmlNodeList SelectNodes( string xpath ); [C++] public: XmlNodeList* SelectNodes( String* xpath ); [JScript] public function SelectNodes( xpath : String ) : XmlNodeList;
参数
-
xpath
- XPath 表达式。
返回值
一个 XmlNodeList,包含匹配 XPath 查询的节点集合。
异常
异常类型 | 条件 |
---|---|
XPathException | XPath 表达式包含前缀。 |
备注
如果 XPath 表达式需要命名空间解析,必须使用接受 XmlNamespaceManager 作为参数的 SelectNodes 重载。XmlNamespaceManager 用于解析命名空间。
注意如果 XPath 表达式不包含前缀,则假定命名空间 URI 为空命名空间。如果 XML 包含默认命名空间,则您仍必须使用 XmlNamespaceManager 并向其添加前缀和命名空间 URI,否则将得不到任何选定的节点。
该方法是文档对象模型 (DOM) 的 Microsoft 扩展。
示例
[VisualBasic,C#,C++] 下面的示例更改所有 Jane Austen 写的书的价格。
[VisualBasic] Imports System 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 [C#] using System; using System.IO; using System.Xml; public class Sample { 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); } } [C++] #using #using using namespace System; using namespace System::IO; using namespace System::Xml; int main() { XmlDocument* doc = new XmlDocument(); doc->Load(S"booksort.xml"); XmlNodeList* nodeList; XmlNode* root = doc->DocumentElement; nodeList=root->SelectNodes(S"descendant::book[author/last-name='Austen']"); //Change the price on the books. System::Collections::IEnumerator* myEnum = nodeList->GetEnumerator(); while (myEnum->MoveNext()) { XmlNode* book = __try_cast(myEnum->Current); book->LastChild->InnerText=S"15.95"; } Console::WriteLine(S"Display the modified XML document...."); doc->Save(Console::Out); }
[VisualBasic,C#,C++] 该示例使用文件 booksort.xml 作为输入。
Jane Austen 24.95 Margaret Atwood 29.95 Jane Austen 19.95 Jane Austen 19.95
[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”