Help with processing xml from an http api return

M

mnvikefan

What would be the best way to process a return from an http api login
function that returns one of the following two XML streams?


Successful login with a session ID returned

<?xml version="1.0" encoding="UTF-8" ?>
<CreateSessionResponse>
<result>0</result>
<warning>CreateSession succeeded</warning>
<return>
<SessionID>1126765227212</SessionID>
</return>
</CreateSessionResponse>


Unsuccessful return with a fault code and message

<?xml version="1.0" encoding="UTF-8" ?>
<CreateSessionResponse>
<fault>
<faultcode>80004005</faultcode>
<faultstring>AgentIntegration error : Login failed. Reason is:
Invalid User Name or Password. Please try again.</faultstring>
</fault>
</CreateSessionResponse>
 
G

Guest

You don't say if this is SOAP or just some sort of "plain XML" response
structure.
However, it looks like SOAP, but if you arent using a WebReference to the
WSDL and a WebService Proxy, it's still easy to see that both responses
represent a complete XmlDocument.

So, to keep things simple, I'd load it into an XmlDocument, and look
for the contents of the CreateSessionResponse node.
if
SelectSingleNode("//CreateSessionResponse/fault/faultcode").InnerText

is not null, you got a SOAP FAULT.

Conversely if

SelectSingleNode("//CreateSessionResponse/return/SessionID").InnerText

is not null, you got a Session.

hope that makes sense.

Peter
 
M

mnvikefan

These are not soap messages. There is no web reference to a wsdl.
However, thankyou for the comments about loading the documents into an
XmlDocument object and using the SelectSingleNode method. I think that
this will help me very much. I was wondering if I should use an
XmlTextReader to get the data and while that would have worked, I don't
think it is as straight forward as the method you just presented me.
 
G

Guest

You can and should consider using an XmlTextReader, as it is more efficient.
I was just trying to bring it down to a "lowest common denominator".
Peter
 
G

GeekCIA

For the most part, your suggestion below is working, but now I am
having a weird issue with the following XML return.

<?xml version="1.0" encoding="UTF-8" ?>
<ActivateWithTextResponse>
<result>0</result>
<warning>ActivateWithText succeeded</warning>
<return>
<NumberConcepts>33</NumberConcepts>
</return>
</ActivateWithTextResponse>

Here is the error segment that I am receiving...

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:


Line 104: {
Line 105: labelMessage.Text =
document.SelectSingleNode("//ActivateWithTextResponse/warning").InnerTex
t;
Line 106: TextBox1.Text =
document.SelectSingleNode("//ActivatetWithTextResponse/result/NumberConc
epts").InnerText;
Line 107: }
Line 108: }


Source File: c:\inetpub\wwwroot\helpdesk\search.aspx.cs Line: 106


Not sure why line 106 is giving me problems and line 105 is not.




You don't say if this is SOAP or just some sort of "plain XML"
response structure.
However, it looks like SOAP, but if you arent using a WebReference to
the WSDL and a WebService Proxy, it's still easy to see that both
responses represent a complete XmlDocument.

So, to keep things simple, I'd load it into an XmlDocument, and look
for the contents of the CreateSessionResponse node.
if
SelectSingleNode("//CreateSessionResponse/fault/faultcode").InnerText

is not null, you got a SOAP FAULT.

Conversely if

SelectSingleNode("//CreateSessionResponse/return/SessionID").InnerText

is not null, you got a Session.

hope that makes sense.

Peter


--
 
N

Nick Hounsome

There is nothing weird about this - you just can't type

"ActivatetWithTextResponse/result/NumberConcepts"
^ ^^^^^^

Always check your paths.

I suggest that you knock up a small app to load a doc and highlight the
nodes selected by a path - it is quite simple and I find it extremely useful
when the expressions get more complicated.
 
N

Nick Hounsome

That didn't format correctly:

1) you have an extra 't' at the end of Activate
2) NumberConcepts is in the return node not the result node
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top