XmlNode.SelectSingleNode中含有引号的处理方法

来源:互联网 发布:柳州广电网络客服电话 编辑:程序博客网 时间:2024/05/16 13:50

http://technet.microsoft.com/zh-cn/library/h0hw012b.aspx


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

选择匹配 XPath 表达式的第一个XmlNode XPath 表达式中的任何前缀都使用提供的 XmlNamespaceManager 进行解析。

命名空间:  System.Xml
程序集:  System.Xml(在 System.Xml.dll 中)
Visual Basic
Public Function SelectSingleNode ( _    xpath As String, _    nsmgr As XmlNamespaceManager _) As XmlNode
C#
public XmlNode SelectSingleNode(    string xpath,    XmlNamespaceManager nsmgr)
Visual C++
public:XmlNode^ SelectSingleNode(    String^ xpath,     XmlNamespaceManager^ nsmgr)
F#
member SelectSingleNode :         xpath:string *         nsmgr:XmlNamespaceManager -> XmlNode 

参数

xpath
类型:System.String
XPath 表达式。
nsmgr
类型:System.Xml.XmlNamespaceManager
一个 XmlNamespaceManager,用于为 XPath 表达式中的前缀解析命名空间。

返回值

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

XPath 表达式包含 XmlNamespaceManager 中没有定义的前缀。

XPath 表达式可以包含命名空间。使用 XmlNamespaceManager 支持命名空间解析。如果 XPath 表达式包含前缀,则必须将前缀和命名空间 URI 对添加到XmlNamespaceManager 中。

注意注意

如果 XPath 表达式不包含前缀,则假定命名空间 URI 为空命名空间。如果 XML 包含默认命名空间,则您仍必须将前缀和命名空间 URI 添加到XmlNamespaceManager 中;否则将得不到选定的节点。有关更多信息,请参见 使用 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"); XmlNode book = doc.SelectSingleNode("//ab:book", nsmgr);
注意注意

使用公式表示 XPath 表达式时的一个常见问题是如何在表达式中包含单引号 (') 或双引号 (")。如果确实要搜索包含单引号的值,则必须用双引号将该字符串括起来。如果需要搜索包含双引号的值,则必须用单引号将该字符串括起来。

例如,假设您有以下 XML:

 <bookstore xmlns="http://www.lucernepublishing.com">   <book>     <title>&apos;Emma&apos;</title>   </book> </bookstore>

下面的 Visual Basic 代码选择一个包含单引号的元素:

 Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable) nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com") book = root.SelectSingleNode("descendant::ab:book[ab:title=""'Emma'""]", nsmgr)

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

下面的示例选择具有匹配的 ISBN 值的书。

Visual Basic
Imports SystemImports System.IOImports System.Xmlpublic 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 the book node with the matching attribute value.      Dim book as XmlNode       Dim root as XmlElement = doc.DocumentElement      book = root.SelectSingleNode("descendant::book[@bk:ISBN='1-861001-57-6']", nsmgr)      Console.WriteLine(book.OuterXml)  end subend 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");      //Create an XmlNamespaceManager for resolving namespaces.      XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);      nsmgr.AddNamespace("bk", "urn:samples");      //Select the book node with the matching attribute value.      XmlNode book;      XmlElement root = doc.DocumentElement;      book = root.SelectSingleNode("descendant::book[@bk:ISBN='1-861001-57-6']", nsmgr);      Console.WriteLine(book.OuterXml);  }}
Visual 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" );   //Create an XmlNamespaceManager for resolving namespaces.   XmlNamespaceManager^ nsmgr = gcnew XmlNamespaceManager( doc->NameTable );   nsmgr->AddNamespace( "bk", "urn:samples" );   //Select the book node with the matching attribute value.   XmlNode^ book;   XmlElement^ root = doc->DocumentElement;   book = root->SelectSingleNode( "descendant::book->Item[@bk:ISBN='1-861001-57-6']", nsmgr );   Console::WriteLine( book->OuterXml );}

该示例使用文件 booksort.xml 作为输入。

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


原创粉丝点击