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?

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
decompose you code in smaller parts... something is null. It's hard to tell with so few code
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.
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;

3 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.