Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML .NET WebService
hello all,
 
I have a webservice that returns this XML
 
<UpdateSalesHeaderResponse xmlns="http://tempuri.org/">
<UpdateSalesHeaderResult>true</UpdateSalesHeaderResult>
</UpdateSalesHeaderResponse>
 
How can i get the value of UpdateSalesHeaderResult tag?
 
I currently do this:
 
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 15-Feb-13 2:31am
IviKAZAZI1.5K
Edited 16-Feb-13 2:17am
v2
Comments
Fred Flams at 15-Feb-13 10:00am
   
Hello, and what is your problem ? That snippet of code should work.
Marcus Kramer at 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 at 16-Feb-13 3:51am
   
Have you tried trimming the content of the InnerText property ?
I mean: doc.SelectSingleNode("//UpdateSalesHeaderResult").InnerText.Trim() == "true"
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think it is the namespace causing an issue (note I don't fully get them so feel free to improve this answer)
 
// 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
}
  Permalink  
Comments
Fred Flams at 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) at 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 at 16-Feb-13 6:17am
   
Fred Its right !
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I ended up using XMLReader:
 
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>
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 16 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100