0

I am receiving a response from a web Request as a string that in fact is an XML, and this response can be different according to the Request that I send. It can be an XML with Success nodes or Error nodes.

What's the best way to handle this in my code?
Can I turn this String as an object to access each node of the response?

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns1:myService xmlns:ns1="http://site.de/alpm">
         <ns1:Response>
               <ns1:OrganisationData>
                  <ns1:ClientId>myID</ns1:ClientId>
                  <ns1:UserId>service</ns1:UserId>
                  <ns1:Pass>myPass</ns1:Pass>
               </ns1:OrganisationData>
               <ns1:TransactionData>
                  <ns1:TrxId>tg0rta1a1-6fh-hfh5-ryyb-ryyt56</ns1:CSDBTrxId>
                  <ns1:TimeOfProcessing>2018-11-28T13:09:41.179Z</ns1:TimeOfProcessing>
               </ns1:TransactionData>          
            <ns1:Error>
               <ns1:ReturnCode>lpt-978-jh</ns1:ReturnCode>
               <ns1:Description>my description</ns1:Description>
            </ns1:Error>
         </ns1:Response>
      </ns1:myService>
   </soap:Body>
</soap:Envelope>

What changes is the <ns1:Error></ns1:Error>, in case of sucess it will be a new tag.

How can I deal with this in my code. Dont forget that I am receiving this as a String? This is the code, the string is "myResult":

using (var webResponse = soapRequest.EndGetResponse(asyncResult))
{
    string myResult;
    var responseStream = webResponse.GetResponseStream();
    if (responseStream == null)
    {
        return null;
    }
    using (var reader = new StreamReader(responseStream))
    {
        myResult = reader.ReadToEnd();
    }
}
Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
JohnnyJoe
  • 15
  • 5

1 Answers1

0

You can include all the possible properties that you expect (in case you will only switch between "Error" and 'NewTag") and continue with parsing the request normally.

As an example on how your class would look like:

public class Response
{
    [XmlElement("OrganisationData")] 
    public OrganisationData OrganisationData { get; set; }

    [XmlElement("TransactionData")] 
    public TransactionData TransactionData { get; set; }

    [XmlElement("Error")] 
    public Error Error { get; set; }

    [XmlElement("NewTag")] 
    public NewTag NewTag { get; set; }
}

Now simply you can check if "Error" or "NewTag" is null after deserializing your XML string and handle each case as needed:

using (var webResponse = soapRequest.EndGetResponse(asyncResult))
{
    string myResult;
    var responseStream = webResponse.GetResponseStream();
    if (responseStream == null)
    {
        return null;
    }
    using (var reader = new StreamReader(responseStream))
    {
        myResult = reader.ReadToEnd();
    }
    var deserializedResult = YourDeserializationLogic(myResult);
    if(deserializedResult != null && deserializedResult.Error != null)
    {
       //Do your error handling Logic
    }
    else if(deserializedResult != null && deserializedResult.NewTag != null)
    {
       //Do your post success logic
    }
}

Note: I have tested out the concept using XmlSerializer

  • what is supposed to be that method? "YourDeserializationLogic"?? and in that class i wont be able to add [XmlRoot(":myService" ...because this value can change depending on the Service im getting the response. And for example it will have to be [XmlElement("nsl:Error")] with the namespace no? – JohnnyJoe Nov 28 '18 at 15:28
  • "YourDeserializationLogic" is The method supposed to have the code to deserialize the XML string into objects, I did not include it as it was not the core of the question. also in your question you stated that the node which will change is the Error node, not the entire XML, so if the entire XML is going to change this is another thing. as for having to include the namespace, I don't think its mandatory, I have tested the solution I provided without including the namespaces and it was working without issues. – AhmedBinGamal Nov 28 '18 at 17:24
  • another thing to avoid all of that, you can simply search if the string contains the error tag or any other tag you need to look for. For example: 'if(myResult.Contains(""){ //Do some stuff}' – AhmedBinGamal Nov 28 '18 at 17:31