473,325 Members | 2,792 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,325 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 8379
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.