1

I have 2 specific questions with regards to passing a System.IO.Stream (from a method) and deserialization into object (another method).

XML Response I get from a WebRequest (please note there are no root tags)

<?xml version="1.0" encoding="UTF-8"?>
<response id="-2d953936:14174bb0cf3:-5213">
      <date>2013-10-01 12:01:55.532999</date>
      <status>
             <current>open</current>
             <next>after</next>
             <change_at>16:00:00</change_at>
      </status>
      <message>Market is open</message>
      <unixtime>1380643315</unixtime>
</response>

Method 1 - ResponseMethod - Currently returning string

private static string GetResponse(HttpWebRequest request)
{
      var v_Response = request.GetResponse();
      var v_DataStream = v_Response.GetResponseStream();

      var v_Reader = new System.IO.StreamReader(v_DataStream);
      var x_XMLResponse = v_Reader.ReadToEnd();

       //Close all Stream logic
       v_Reader.Close(); v_DataStream.Close(); v_Response.Close();

       return x_XMLResponse;
}

Method 2 - Convert the XML to an object

// I would use XDocument and Lin2XML to get my typed object - for example MarketStatus

Questions are:

  1. I am currently passing string from Method 1. That doesnt help me in deserializing from XML to object. Should I be passing the return value as StreamReader and then use that as an input into method 2 to get my typed object. Is that a standard approach or there are better ways to this?
  2. My ultimate objective is that the return value from second method should be an object.

Additional Note:

  1. The reason this functionality is broken into 2 methods because I want the web response method and deserailization separate for testing purposes.
  2. I don't have an XSD but have created a MarketStatus Class

Any code snippets/suggestions will really appreciate

Nissa
  • 4,636
  • 8
  • 29
  • 37
Patrick
  • 864
  • 1
  • 14
  • 27

2 Answers2

3

We typically use a generic method, similar to the following (simplified for posting), which uses the XMLSerializer to deserialize an XML string representation into the corresponding object.

    public T ReturnObjectfromXml<T>(string xmlForm)
    {
        XmlSerializer xs = new XmlSerializer(typeof(T));
        StringReader sr = new StringReader(xmlForm);
        XmlTextReader xts = new XmlTextReader(sr);
        return ((T)xs.Deserialize(xts));
    }

Hope this helps.

Regards,

Seymour
  • 7,043
  • 12
  • 44
  • 51
  • thanks for this generic class. It actually works but the problem is see my xml above - it doesnt have xmlns and hence i really cant deseriliaze it. That leaves me using LINQ2XML code here but then it no longer is generic method. Any suggestions on that? Please note I have accepted your answer since it is in the right spirit but doesnt work for me since I dont have the full formed XML – Patrick Oct 02 '13 at 02:30
0

It looks like you are doing some type of message passing. You will be better off using WCF or ASP.NET Web API instead of rolling your own infrastructure code (to serialize and de-serialize).

To answer question 1: No, it is better to return a string and dispose of the reader as soon as you are done with it. See When should I dispose my objects in .NET?

Comment on note 1: In most cases, you wouldn't want to unit test the serialization/deserialization.

Community
  • 1
  • 1
an phu
  • 1,823
  • 1
  • 16
  • 10