Connecting Tech Pros Worldwide Help | Site Map

XPath query on XmlNode

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 26th, 2006, 12:05 PM
Claudio
Guest
 
Posts: n/a
Default XPath query on XmlNode

Hi
I'm trying to use XPath queries with streaming XML, but I cannot make
it working.
The solution I'm trying to implement is: create a XmlNode as soon as I
have a full XML element and use the SelectSingleNode method with the
XPath query

Here is a test console application i wrote to better demonstrate my
issue and.. help you help me :)

--------------
using System;
using System.Text;
using System.IO;
using System.Xml;
using System.Diagnostics;

class xpath
{
static void Main(string[] args)
{
//INITIALIZATION
//create the xml reader
Stream stream = new MemoryStream(
Encoding.UTF8.GetBytes(
"<iq><bind xmlns='urn:long:namespace'><jid>MYVALUE</jid></bind></iq>")
, false);
XmlReaderSettings xmlRsettings = new XmlReaderSettings();
xmlRsettings.ConformanceLevel = ConformanceLevel.Fragment;
xmlRsettings.IgnoreComments = true;
XmlReader xmlReader = XmlReader.Create(stream, xmlRsettings);

//move to the first element (<iq>)
xmlReader.Read();
XmlDocument doc = new XmlDocument();
//END INITIALIZATION

XmlNode node = doc.ReadNode(xmlReader);

//here I would like to read MYVALUE
//this is OK

Debug.WriteLine(node.ChildNodes[0].ChildNodes[0].ChildNodes[0].Value);
//but I'd rather using a XPath query like
XmlNode val = node.SelectSingleNode("iq/bind/jid/text()");
Debug.WriteLine(val == null ? "null" : val.Value);
}
}

--------------
Any idea on what I'm doing wrong? Do you see other approaches?
Thank you


  #2  
Old November 26th, 2006, 12:25 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: XPath query on XmlNode

Claudio wrote:
Quote:
Stream stream = new MemoryStream(
Encoding.UTF8.GetBytes(
"<iq><bind xmlns='urn:long:namespace'><jid>MYVALUE</jid></bind></iq>")
, false);
The bind element and its child jid element are in the namespace with the
URI urn:long:namespace which means to do XPath on that you nead an
XmlNamespaceManager instance, bind a prefix to the URI, use that prefix
in your XPath expressions and pass the namespace manager as the second
argument of SelectNodes/SelectSingleNode e.g.

XmlNamespaceManager namespaceManager = new
XmlNamespaceManager(doc.NameTable);
namespaceManager.AddNamespace("pf", "urn:long:namespace");
XmlNode val = node.SelectSingleNode("pf:bind/pf:jid/text()",
namespaceManager);


Note that the XPath expression needed to be corrected in your post.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  #3  
Old November 26th, 2006, 12:45 PM
Claudio
Guest
 
Posts: n/a
Default Re: XPath query on XmlNode

Thank you very much for the quick and perfect reply!

Martin Honnen wrote:
Quote:
The bind element and its child jid element are in the namespace with the
URI urn:long:namespace which means to do XPath on that you nead an
XmlNamespaceManager instance, bind a prefix to the URI, use that prefix
in your XPath expressions and pass the namespace manager as the second
argument of SelectNodes/SelectSingleNode e.g.
>
XmlNamespaceManager namespaceManager = new
XmlNamespaceManager(doc.NameTable);
namespaceManager.AddNamespace("pf", "urn:long:namespace");
XmlNode val = node.SelectSingleNode("pf:bind/pf:jid/text()",
namespaceManager);
>
>
Note that the XPath expression needed to be corrected in your post.
>
--
>
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.