How to deal with xmlnode. selectsinglenode containing quotation marks

Source: Internet
Author: User

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

. NET Framework class library xmlnode. selectsinglenode method (string, xmlnamespacemanager)

Select the first matching XPath expression
Xmlnode.
Any prefix in the XPath expression is parsed using the provided xmlnamespacemanager.

Namespace:System. xml
Assembly:System. XML (in system. xml. dll)

Syntax

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 
Parameters
Xpath
Type: system. String
The XPath expression.
Nsmgr
Type: system. xml. xmlnamespacemanager
One
Xmlnamespacemanager, used to resolve the namespace for the prefix in the XPath expression.
Return Value

Type: system. xml. xmlnode
The first one matching the XPath Query
Xmlnode; if no matching node is found, it is a null reference (in Visual Basic
Nothing ).
The xmlnode cannot be connected to the XML document in real time.
That is to say, changes in the XML document will not appear in xmlnode, and vice versa.

Exception
Exception Condition
Xpathexception

XPath expressions include
The prefix is not defined in xmlnamespacemanager.

Remarks

An XPATH expression can contain namespaces.
Use xmlnamespacemanager to support namespace resolution.
If an XPATH expression contains a prefix, you must add the prefix and namespace URI pair
Xmlnamespacemanager.

Note:

If an XPATH expression does not contain a prefix, it is assumed that the namespace URI is null.
If XML contains the default namespace, you must add the prefix and namespace URI
Xmlnamespacemanager; otherwise, no node is selected.
For more information, see
Select a node using the XPath navigation bar.

For example, if you have the following XML:

 <bookstore xmlns="http://www.lucernepublishing.com">  <book>    <title>Pride And Prejudice</title>  </book> </bookstore>

The following C # code selects the first book node:

 XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable); nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com"); XmlNode book = doc.SelectSingleNode("//ab:book", nsmgr);
Note:

A common problem when using formulas to represent XPath expressions is how to include single quotation marks (') or double quotation marks (") in an expression (").
To search for a value that contains single quotes, you must enclose the string with double quotation marks.
To search for a value that contains double quotation marks, you must enclose the string with single quotation marks.

For example, assume that you have the following XML:

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

The following Visual Basic code selects an element that contains single quotes:

 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)

This method is Microsoft extension of the Document Object Model (DOM.

Example

The following example selects a book with a matched ISBN value.

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

This example uses a file
Booksort. XML is used as the input.

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.