SelectSingleNode 方法 (String)
.NET Framework 类库
XmlNode.SelectSingleNode 方法 (String)

选择匹配 XPath 表达式的第一个 XmlNode

命名空间:System.Xml
程序集:System.Xml(在 system.xml.dll 中)

Visual Basic(声明)
Public Function SelectSingleNode ( _
    xpath As String _
) As XmlNode
Visual Basic(用法)
Dim instance As XmlNode
Dim xpath As String
Dim returnValue As XmlNode

returnValue = instance.SelectSingleNode(xpath)
C#
public XmlNode SelectSingleNode (
    string xpath
)
C++
public:
XmlNode^ SelectSingleNode (
    String^ xpath
)
J#
public XmlNode SelectSingleNode (
    String xpath
)
JScript
public function SelectSingleNode (
    xpath : String
) : XmlNode

参数

xpath

XPath 表达式。

返回值

与 XPath 查询匹配的第一个 XmlNode;如果未找到任何匹配节点,则为 空引用(在 Visual Basic 中为 Nothing)。不应该要求将 XmlNode “实时”连接到 XML 文档。也就是说,XML 文档中的更改不会出现在 XmlNode 中,反之亦然。
异常类型条件

XPathException

XPath 表达式包含前缀。

如果 XPath 表达式需要命名空间解析,必须使用接受 XmlNamespaceManager 作为参数的 SelectSingleNode 重载。XmlNamespaceManager 用于解析命名空间。

Note注意

如果 XPath 表达式不包含前缀,则假定命名空间 URI 为空命名空间。如果 XML 包含默认命名空间,则您仍必须使用 XmlNamespaceManager 并向其添加前缀和命名空间 URI;否则将得不到选定的节点。有关更多信息,请参见 使用 XPath 导航选择节点

该方法是文档对象模型 (DOM) 的 Microsoft 扩展。

下面的示例更改第一本 Jane Austen 写的书的价格。

Visual Basic
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 root as XmlNode = doc.DocumentElement

    book=root.SelectSingleNode("descendant::book[author/last-name='Austen']")
 
    'Change the price on the book.
    book.LastChild.InnerText="15.95"

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

    XmlNode book;
    XmlNode root = doc.DocumentElement;

    book=root.SelectSingleNode("descendant::book[author/last-name='Austen']");
 
    //Change the price on the book.
    book.LastChild.InnerText="15.95";

    Console.WriteLine("Display the modified XML document....");
    doc.Save(Console.Out);    
  }
}
C++
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->Load( "booksort.xml" );
   XmlNode^ book;
   XmlNode^ root = doc->DocumentElement;
   book = root->SelectSingleNode( "descendant::book[author/last-name='Austen']" );
   
   //Change the price on the book.
   book->LastChild->InnerText = "15.95";
   Console::WriteLine( "Display the modified XML document...." );
   doc->Save( Console::Out );
}

J#
import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("booksort.xml");

        XmlNode book;
        XmlNode root = doc.get_DocumentElement();

        book = 
            root.SelectSingleNode("descendant::book[author/last-name='Austen']");

        //Change the price on the book.
        book.get_LastChild().set_InnerText("15.95");

        Console.WriteLine("Display the modified XML document....");
        doc.Save(Console.get_Out());
    } //main
} //Sample

该示例使用文件 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>

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
Page view tracker