473,234 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,234 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 8372
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mark Snelling | last post by:
Hi, I have a question regarding XPath and namespaces. I have a piece of XML... <Cred> <Meta> <Type xmlns='syncml:metinf'>syncml:auth-basic</Type> </Meta> <Data>QnJ1Y2UyOk9oQmVoYXZl</Data>...
5
by: Gnic | last post by:
Hi , I have an XmlDocument instance, I want to find a node in the xml, but I don't know it's path until runtime, for example <aaa> <bbb name="x"/> <aaa attr="y"> <ccc>sometext</ccc> </aaa>
3
by: beachboy | last post by:
can we get the max value of element from xpath e.g: Xpath Query Expression: /Books/Book to select XMLNode which id is "3" Can I use Xpath to get the MAX Book id? Thanks in advanced.
0
by: dotnetnoob | last post by:
Dim strFacilitFile As String = Func_Facilit(strJobSite) Dim xTempltDoc As New Xml.XmlDocument Dim xGpDoc As New Xml.XmlDocument Dim y As Integer = 0 'Dim intNxtInstNumb As Integer =...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
2
by: hary | last post by:
Hello, I have a C# application that is consuming a C# WebService. I am calling a method on the WebService and it is sending me back a response which contains an XmlDocument: string soapMessage...
3
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
Hi; We have a TreeView that represents an xml file (or it's schema is a more accurate statement). We want to have a double click on a node in the tree generate the XPath to get to that node. ...
0
by: sara77 | last post by:
I have a web service that receives an xml Node and xpath and should send back the matching node. My code works fine, the only problem is when the matching node is an XmlAttribute then it throws the...
5
by: coolnags | last post by:
I have an array of XmlNode which is returned after deserializing xml document. I have to search particlular XmlNode in the array based on the attribute "instrument" of Record XmlNode. Sample node...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.