471,595 Members | 2,043 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

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

Nov 26 '06 #1
2 8315
Claudio wrote:
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/
Nov 26 '06 #2
Thank you very much for the quick and perfect reply!

Martin Honnen wrote:
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/
Nov 26 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mark Snelling | last post: by
5 posts views Thread by Gnic | last post: by
3 posts views Thread by beachboy | last post: by
reply views Thread by dotnetnoob | last post: by
3 posts views Thread by Jason Mobarak | last post: by
3 posts views Thread by =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.