|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
XPath Syntaxthe 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); 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 wrote:
> Can anyone tell me what's wrong with the syntax of the XPath query in As you do not get a syntax error there is nothing wrong syntactically > 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). 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 wrote:
Show quote > Can anyone tell me what's wrong with the syntax of the XPath query in Please also include the full XML in order for us to properly evaluate > 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; the problem and offer help. -- Tom Porterfield 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 wrote:
> Thanks for the replies, here is the response I'm receiving: To make your original code work you need to add> > <?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> 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 wrote:
> Why the need to add the "df" prefix? I haven't ran across that yet in Your original XPath HelloWorldResponse/HelloWorldResult selects elements > any documentation. 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. 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. -- Show quote- Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com "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. >
Other interesting topics
|
|||||||||||||||||||||||