Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello all,

I have a webservice that returns this XML

HTML
<UpdateSalesHeaderResponse xmlns="http://tempuri.org/">
<UpdateSalesHeaderResult>true</UpdateSalesHeaderResult>
</UpdateSalesHeaderResponse>


How can i get the value of UpdateSalesHeaderResult tag?

I currently do this:

C#
XmlDocument doc = new XmlDocument();

                    // Load data  
                    doc.Load("http://localhost/xxxxx/Service1.svc/xxxxxx");

                    // 

                    if (doc.SelectSingleNode("//UpdateSalesHeaderResult").InnerText == "true")
                    {
                        //do smth
                    }
                    else
                    {
                        //do smth
                    }


the error says :"Object reference not set to an instance of an object." at this point doc.SelectSingleNode("//UpdateSalesHeaderResult").InnerText
Posted
Updated 16-Feb-13 1:17am
v2
Comments
Fred Flams 15-Feb-13 10:00am    
Hello, and what is your problem ? That snippet of code should work.
fjdiewornncalwe 15-Feb-13 10:12am    
I agree with Fred. This code should work.
Have you debugged your code to make sure you are getting back the xml that expect? That's about the only thing here to check right now that I could suggest.
Fred Flams 16-Feb-13 3:51am    
Have you tried trimming the content of the InnerText property ?
I mean: doc.SelectSingleNode("//UpdateSalesHeaderResult").InnerText.Trim() == "true"

I think it is the namespace causing an issue (note I don't fully get them so feel free to improve this answer)

C#
// Load data
doc.Load("http://localhost/xxxxx/Service1.svc/xxxxxx");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("tu", @"http://tempuri.org/");

if (doc.SelectSingleNode("//tu:UpdateSalesHeaderResult", nsmgr).InnerText == "true")
{
    //do smth
}
else
{
    //do smth
}
 
Share this answer
 
Comments
Fred Flams 16-Feb-13 3:50am    
Well actually, I d'ont think the namespace can be the cause of the problem since tempuri.org is the default xml namespace for all xml document. Plus this is enforced by the opening tag where the namespace is specified using xmlns without anything else, that alone declares a default namespace.
Chris Reynolds (UK) 16-Feb-13 4:12am    
And that's what I thought too, hence my hesitancy in the answer, thanks for confirming. But I pasted the OP's code and XML into VS and the only way I could get the SelectSingleNode to work was to include the namespace. Feel free to have a dig at it yourself as I'm away from work now and can't get at the code I hacked together yesterday
IviKAZAZI 16-Feb-13 6:17am    
Fred Its right !
I ended up using XMLReader:

C#
XmlTextReader reader = new XmlTextReader("http://localhost/xxxxxxxx/Service1.svc/xxxxx<pre lang="cs">&quot;);

              // Skip non-significant whitespace
              reader.WhitespaceHandling = WhitespaceHandling.Significant;

              // Read nodes one at a time
              while (reader.Read())
              {
                 if(reader.Value == &quot;true&quot;)
                 {
                     //do smth
                 }
              if(reader.Value == &quot;false&quot;)
                 {
                     //do smth
                 }

              }</pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900