0

I've tried several methods, from Linq to loading the data to an XML document, but i can't seem to be able to return the result that i need.

here's the example XML:

<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service" xmlns:com="http://www.webex.com/schemas/2002/06/common" xmlns:event="http://www.webex.com/schemas/2002/06/service/event"><serv:header><serv:response><serv:result>SUCCESS</serv:result><serv:gsbStatus>PRIMARY</serv:gsbStatus></serv:response></serv:header><serv:body><serv:bodyContent xsi:type="event:createEventResponse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><event:sessionKey>11111111</event:sessionKey><event:guestToken>111111111111111111111</event:guestToken></serv:bodyContent></serv:body></serv:message>

And, here's what i've tried to do:

StreamReader reader = new StreamReader(dataStream);
XmlDocument doc = new XmlDocument();
doc.LoadXml(reader.ReadToEnd());
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
XmlNamespaceManager ns2 = new XmlNamespaceManager(doc.NameTable);
XmlNamespaceManager ns3 = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("serv", "http://www.webex.com/schemas/2002/06/service");
ns2.AddNamespace("com", "http://www.webex.com/schemas/2002/06/common");
ns3.AddNamespace("event", "http://www.webex.com/schemas/2002/06/service/event");
XmlNode node = doc.SelectSingleNode("result",ns);

Yet, for some reason i cannot ever seem to return the actual result, which should be either 'SUCCESS' or 'FAILURE' based on the actual xml above.

How can i do this?

Phillip
  • 3
  • 1
  • 2

2 Answers2

2

your xpath query is not correct.

try this one :

XmlNode node = doc.SelectSingleNode("//serv:result",ns);

or

XmlNode node = doc.SelectSingleNode("serv:message/serv:header/serv:response/serv:result",ns);
Steve B
  • 36,818
  • 21
  • 101
  • 174
  • Thank you, the 2nd one worked for me, however when i do node["serv:result"].Value.ToString() it doesn't seem to want to return the actual result. Should i be doing this with Innertext to return the result? – Phillip Feb 08 '11 at 23:29
  • It gives a A first chance exception of type 'System.NullReferenceException' occurred in WeBeX Demo.exe System.NullReferenceException: Object reference not set to an instance of an object. – Phillip Feb 08 '11 at 23:40
  • decompose you code in smaller parts... something is null. It's hard to tell with so few code – Steve B Feb 08 '11 at 23:41
  • Here's the code i used: XmlNode node = doc.SelectSingleNode("serv:message/serv:header/serv:response/serv:result",ns); if (node != null) { Console.WriteLine("not null"); Console.WriteLine(node.Value.ToString()); } else { Console.WriteLine("null"); } It returns: not null A first chance exception of type 'System.NullReferenceException' occurred in WeBeX Demo.exe System.NullReferenceException: Object reference not set to an instance of an object. – Phillip Feb 08 '11 at 23:48
0

This works:

XDocument doc = XDocument.Load(@"test.xml");
XNamespace serv = "http://www.webex.com/schemas/2002/06/service";
var result = doc.Descendants(serv + "result").First().Value;
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • when trying to write 'var' to the command line, i get an error with your code there: A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll System.ArgumentException: Illegal characters in path. Tried changing it to a string as well, but same result. – Phillip Feb 08 '11 at 23:38
  • I tested this code with your XML as input and it works perfectly, `result` is actually of type `string`, you can use it as such, i.e. `Console.WriteLine(result)` – BrokenGlass Feb 08 '11 at 23:42
  • i see what i did wrong. I used XDocument.Load() instead of XDocument.Parse() considering i was using it from a stream reader. Works great, returned exactly what i needed! Thank you so much! – Phillip Feb 08 '11 at 23:55