views:

263

answers:

2

I am calling a REST webservice.

The response looks something like this:

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<data>
  <status>1</status>
  <message>OK</message>
  <results>
    <result>
      <account>12345</account>
      <to>012345678</to>
      <from>054321</from>
      <message>Testing</message>
      <flash></flash>
      <replace></replace>
      <report></report>
      <concat></concat>
      <id>f8d3eea1cbf6771a4bb02af3fb15253e</id>
    </result>
  </results>
</data>

I have a class called "SMSSendingResponse" which looks like this:

public class SMSSendingResponse
{
    public string AccountNumber { get; set; }
    public string Status { get; set; }
    public string Message { get; set; }
    public string ResponseID { get; set; }
    public SMSMessage SMSMessage { get; set; }
}

SMSMessage looks like this:

public class SMSMessage
{
    public string To { get; set; }
    public string From { get; set; }
    public string Message { get; set; }
}

As you can see, I'm ignoring some of the returned elemenst (flash, replace etc..)

What's the best way of me serializing the returned XML into this object?

I tried using XmlSerializer, but this threw an error... I guess because I'm not serializing first using XmlSerializer.

If it were Json, I'd use the NewtonSoft.Json library... Whilst i suppose i could convert xml to json, then serialize that way, is there a better way?

+1  A: 

You can use XmlSerializer just fine as long as you add the appropriate attributes to the fields of your classes. Check these out for an example of some. You will undoubtedly run into errors the first few times you work with it, but stick with it because once you figure it out, it is a great tool.

EDIT:

Here is a better place to find all of the available attributes. Look through the list for anything that begins with Xml and ends with Attribute. You will want to know them all.

NickLarsen
A: 

You can use xsd.exe to extract schema from the xml file and generate corresponding c# class. You can use the generated class for serializing and deserializing the xml. If you show the code we will be able to identify mistake more easily.

Giorgi