How to map network response into a C# class?

66 views Asked by At

I'm trying to communicate with a server over TCP/IP protocol. Here is my method:

private bool SendTcpRequest<T>(T request, Settings settings)
    {
        try
        {
            TcpClient client = new TcpClient(settings.Url, settings.Port);

            byte[] data = null;

            if (!string.IsNullOrEmpty(request.ToString()))
                data = Encoding.ASCII.GetBytes(request.ToString());
            NetworkStream stream = client.GetStream();

            if (stream.CanWrite)
                stream.Write(data, 0, data.Length);
            else
            {
                return false;
            }

            if (stream.CanRead)
            {
                byte[] myReadBuffer = new byte[client.ReceiveBufferSize];
                IFormatter formatter = new BinaryFormatter();

                do
                {
                    var numberOfBytesRead = stream.Read(myReadBuffer, 0, client.ReceiveBufferSize);

                    responseMessage = new StringBuilder();
                    responseMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
                }
                while (stream.DataAvailable);
            }
            else
            {
                return false;
            }
            stream.Close();
            client.Close();
            return true;
        }
        catch (ArgumentNullException e)
        {
            return false;
        }
        catch (SocketException e)
        {
            return false;
        }
    }

The server will send response like bellow:

<TOPUPRESPONSE>
     <RESULT>00</RESULT> // status of the transaction
     <RESULTTEXT>transaction successful</RESULTTEXT> //description of the result code
     <TERMINALID>69500002</TERMINALID>
     <TXID>0803170919092</TXID>
     <PRODUCTID>XYZ</PRODUCTID>
</TOPUPRESPONSE>

Now I have a c# class for response, I'm wondering how I can map the network response to my class. I'm pretty new in this, I'm not even sure how my responseMessage will look like? Can anyone give some idea please?

1

There are 1 answers

0
Monolithcode On

Create a class based on that repose if that's what your working with:

[XmlRoot(ElementName="TOPUPRESPONSE")]
    public class TOPUPRESPONSE {
        [XmlElement(ElementName="RESULT")]
        public string RESULT { get; set; }
        [XmlElement(ElementName="RESULTTEXT")]
        public string RESULTTEXT { get; set; }
        [XmlElement(ElementName="TERMINALID")]
        public string TERMINALID { get; set; }
        [XmlElement(ElementName="TXID")]
        public string TXID { get; set; }
        [XmlElement(ElementName="PRODUCTID")]
        public string PRODUCTID { get; set; }
    }

Then just deserialize it to work with the data as a class:

XmlSerializer serializer = new XmlSerializer(typeof(TOPUPRESPONSE ));
MemoryStream memStream = new    MemoryStream(Encoding.UTF8.GetBytes(inputString));
msg resultingMessage = (TOPUPRESPONSE)serializer.Deserialize(memStream);

or use a StringReader:

XmlSerializer serializer = new XmlSerializer(typeof(TOPUPRESPONSE));
StringReader rdr = new StringReader(inputString);
msg resultingMessage = (TOPUPRESPONSE)serializer.Deserialize(rdr);