Home All Groups Group Topic Archive Search About
Author
27 Nov 2007 3:22 PM
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.

Author
27 Nov 2007 3:28 PM
Martin Honnen
Looch wrote:
> 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.


--

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/
Author
27 Nov 2007 3:32 PM
Tom Porterfield
Looch wrote:
Show quote
> 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.
--
Tom Porterfield
Author
27 Nov 2007 4:01 PM
Looch
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.)
Author
27 Nov 2007 4:13 PM
Martin Honnen
Looch wrote:
> 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)


--

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/
Author
27 Nov 2007 4:29 PM
Looch
That worked, thank you Martin.

Why the need to add the "df" prefix? I haven't ran across that yet in
any documentation.
Author
27 Nov 2007 4:47 PM
Marc Gravell
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
Author
27 Nov 2007 4:49 PM
Martin Honnen
Looch wrote:

> 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.





--

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/
Author
27 Nov 2007 3:33 PM
Nicholas Paldino [.NET/C# MVP]
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.


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

Show quote
"Looch" <lucianoj2***@yahoo.com> wrote in message
news:c429ec19-584c-4381-bd99-c836c628023b@b40g2000prf.googlegroups.com...
> 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.
>

AddThis Social Bookmark Button