473,770 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 SelectSingleNod e 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.Diagnost ics;

class xpath
{
static void Main(string[] args)
{
//INITIALIZATION
//create the xml reader
Stream stream = new MemoryStream(
Encoding.UTF8.G etBytes(
"<iq><bind xmlns='urn:long :namespace'><ji d>MYVALUE</jid></bind></iq>")
, false);
XmlReaderSettin gs xmlRsettings = new XmlReaderSettin gs();
xmlRsettings.Co nformanceLevel = ConformanceLeve l.Fragment;
xmlRsettings.Ig noreComments = true;
XmlReader xmlReader = XmlReader.Creat e(stream, xmlRsettings);

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

XmlNode node = doc.ReadNode(xm lReader);

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

Debug.WriteLine (node.ChildNode s[0].ChildNodes[0].ChildNodes[0].Value);
//but I'd rather using a XPath query like
XmlNode val = node.SelectSing leNode("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 8400
Claudio wrote:
Stream stream = new MemoryStream(
Encoding.UTF8.G etBytes(
"<iq><bind xmlns='urn:long :namespace'><ji d>MYVALUE</jid></bind></iq>")
, false);
The bind element and its child jid element are in the namespace with the
URI urn:long:namesp ace which means to do XPath on that you nead an
XmlNamespaceMan ager 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/SelectSingleNod e e.g.

XmlNamespaceMan ager namespaceManage r = new
XmlNamespaceMan ager(doc.NameTa ble);
namespaceManage r.AddNamespace( "pf", "urn:long:names pace");
XmlNode val = node.SelectSing leNode("pf:bind/pf:jid/text()",
namespaceManage r);
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:namesp ace which means to do XPath on that you nead an
XmlNamespaceMan ager 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/SelectSingleNod e e.g.

XmlNamespaceMan ager namespaceManage r = new
XmlNamespaceMan ager(doc.NameTa ble);
namespaceManage r.AddNamespace( "pf", "urn:long:names pace");
XmlNode val = node.SelectSing leNode("pf:bind/pf:jid/text()",
namespaceManage r);
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
1366
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> </Cred>
5
7932
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
23948
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
1287
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 = NextInstanceNumber(strJobSite) Dim intNxtInstNumb As Integer = FacilitNxtInstNum(strFacilitFile) Do Dim intGpFlag As Integer If CInt(arlsGpYN.Item(y)) = 1 Then Dim intGpYNFlag As Integer = CInt(arlsGpYN.Item(intGpFlag))
3
4980
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 version="1.0" encoding="utf-8" ?> <thing xmlns="urn:thing-schema-v1"> <foo>foo thing</foo> <bar>bar thing</bar>
2
7304
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 = "<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><validateStudent...
3
2630
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. For the full tree it's easy. We walk up the parents getting the name of each node and put a / between each so we end up with /node1/node2 or node1/node2/@attr1
0
1334
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 following exception: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Cannot write a...
5
8390
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 lookes like this <Record instrument="VOD-US,USD,NORM,R"> <Field name="variant" type="2">R</Field> <Field name="hi" type="5">19.28</Field> </Record> any help will be appreciated.
0
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.