XPath Syntax

  • Thread starter Thread starter Looch
  • Start date Start date
L

Looch

Can anyone tell me what's wrong with the syntax of the XPath query in
the SelectSingleNode parameter?

I keep getting an "Object reference not set to an instance of an
object" error. I've tried none, one and two forward slashes in front
of 'soap:Envelope' and still the same message. (The last commented out
line works so I know the response is well formed/correct).

XmlNamespaceManager nsmr = new XmlNamespaceManager (xdoc.NameTable);
nsmr.AddNamespace ("soap","http://schemas.xmlsoap.org/soap/
envelope/");
XmlNode = result;
XmlElement root = xdoc.DocumentElement;
result = root.SelectSingleNode("/soap:Envelope/soap:Body/
HelloWorldResponse/HelloWorldResult",nsmr);
txtResultValue.text = result.InnerXml;
//txtResultValue.Text =
xdoc.DocumentElement.FirstChild.FirstChild.FirstChild.InnerXml;


Thanks in advance.
 
Looch said:
Can anyone tell me what's wrong with the syntax of the XPath query in
the SelectSingleNode parameter?

I keep getting an "Object reference not set to an instance of an
object" error. I've tried none, one and two forward slashes in front
of 'soap:Envelope' and still the same message. (The last commented out
line works so I know the response is well formed/correct).

As you do not get a syntax error there is nothing wrong syntactically
with your XPath expression. However the XPath expression does not select
a node in your xdoc. You will have to show the XML document if you need
further help.
 
Looch said:
Can anyone tell me what's wrong with the syntax of the XPath query in
the SelectSingleNode parameter?

I keep getting an "Object reference not set to an instance of an
object" error. I've tried none, one and two forward slashes in front
of 'soap:Envelope' and still the same message. (The last commented out
line works so I know the response is well formed/correct).

XmlNamespaceManager nsmr = new XmlNamespaceManager (xdoc.NameTable);
nsmr.AddNamespace ("soap","http://schemas.xmlsoap.org/soap/
envelope/");
XmlNode = result;
XmlElement root = xdoc.DocumentElement;
result = root.SelectSingleNode("/soap:Envelope/soap:Body/
HelloWorldResponse/HelloWorldResult",nsmr);
txtResultValue.text = result.InnerXml;
//txtResultValue.Text =
xdoc.DocumentElement.FirstChild.FirstChild.FirstChild.InnerXml;

Please also include the full XML in order for us to properly evaluate
the problem and offer help.
 
Looch,

Well, without seeing the XML that you are loading, it's impossible to
say.

Also, the code you put in the post doesn't compile (specifically, the
"XmlNode = result;" line). You should address that as well.
 
Thanks for the replies, here is the response I'm receiving:

<?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><HelloWorldResponse xmlns="http://
tempuri.org/"><HelloWorldResult>Hello World.</HelloWorldResult></
HelloWorldResponse></soap:Body></soap:Envelope>

(That should be 'XmlNode result;', not sure how that equal sign got in
there.)
 
Looch said:
Thanks for the replies, here is the response I'm receiving:

<?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><HelloWorldResponse xmlns="http://
tempuri.org/"><HelloWorldResult>Hello World.</HelloWorldResult></
HelloWorldResponse></soap:Body></soap:Envelope>

To make your original code work you need to add

msmr.AddNamespace("df", "http://tempuri.org/");

and then use the following XPath

xdoc.SelectSingleNode("soap:Envelope/soap:Body/df:HelloWorldResponse/df:HelloWorldResult",
nsmr)
 
That worked, thank you Martin.

Why the need to add the "df" prefix? I haven't ran across that yet in
any documentation.
 
There is nothing special about "df" - it is just an alias to the
namespace you need (since no alias is declared earlier).

The most common case when you need this is to reference the /default/
namespace (which is often without a paired alias), which is probably
where Martin's "df" came from. But any alias not currently in use
would work.

Marc
 
Looch said:
Why the need to add the "df" prefix? I haven't ran across that yet in
any documentation.

Your original XPath HelloWorldResponse/HelloWorldResult selects elements
with those names in _no_ namespace while those elements belong to a
namespace:


<HelloWorldResponse xmlns="http://tempuri.org/"><HelloWorldResult>Hello
World.</HelloWorldResult></
HelloWorldResponse>

XML allows you to apply a default namespace using
xmlns="http://tempuri.org/" but XPath 1.0 does not know about default
namespaces, instead you need to bind a prefix to that namespace and use
that prefix to qualify element names.

You can choose any prefix you like but you need to add one and use it
for those elements as they belong to a namespace.
 
Back
Top