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