2

I have this xml feed from an API with a XML sequence.

<?xml version="1.0" encoding="UTF-8" ?>
<Function>
    <Cmd>2002</Cmd>
    <Status>1</Status>
    <Cmd>2003</Cmd>
    <Status>0</Status>
    <Cmd>2004</Cmd>
    <Status>0</Status>
    <Cmd>1012</Cmd>
    <Status>3</Status>
    <Cmd>2006</Cmd>
    <Status>0</Status>
    <Cmd>2007</Cmd>
    <Status>0</Status>
    ...
</Function>

I already tried a few options for deserialization with Restsharp. Ideally I would like to have something like the following, but it's obviously not working.

public class MyResponse
{
    public List<Setting> Settings { get; set;}
}

public class Setting
{
    public int Cmd { get; set; }
    public int Status { get; set; }
}

Thank you

Diego Ferri
  • 2,657
  • 2
  • 27
  • 35

2 Answers2

2

You can use the DotNetXmlDeserializer of RestSharp to make Microsoft's XmlSerializer do the actual deserialization. Define your MyResponse class as follows, using XML serialization attributes to specify element names and also special handling for the Cmd/Status alternating sequence of elements:

[XmlRoot("Function")]
public class MyResponse
{
    [XmlIgnore]
    public List<Setting> Settings { get; set; }

    /// <summary>
    /// Proxy property to convert Settings to an alternating sequence of Cmd / Status elements.
    /// </summary>
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    [XmlAnyElement]
    public XElement[] Elements 
    {
        get
        {
            if (Settings == null)
                return null;
            return Settings.SelectMany(s => new[] { new XElement("Cmd", s.Cmd), new XElement("Status", s.Status) }).ToArray();
        }
        set
        {
            if (value == null)
                Settings = null;
            else
                Settings = value.Where(e => e.Name == "Cmd").Zip(value.Where(e => e.Name == "Status"), (cmd, status) => new Setting { Cmd = (int)cmd, Status = (int)status }).ToList();
        }
    }
}

Then deserialize as follows:

        var serializer = new DotNetXmlDeserializer();
        var myResponse = serializer.Deserialize<MyResponse>(response);

Prototype fiddle.

dbc
  • 104,963
  • 20
  • 228
  • 340
0

Your XML doesn't match your object model. There are two simple ways of fixing it * Make your XML response actually contain a list structure * Write a custom parser. ** https://github.com/restsharp/RestSharp/wiki/Deserialization

Who is responsible for generating the XML / Soap? Looks like something hand crafted.

Look at the examples on the Restsharp page regarding the deserialisation of a list:

<?xml version="1.0" encoding="utf-8" ?>
<NestedListSample>
    <images>
        <image src="1.gif">value1</image>
        <image src="2.gif">value2</image>
        <image src="3.gif">value3</image>
        <image src="4.gif">value4</image>
    </images>
</NestedListSample>

Will map to the following C# schema:

public class ListSample
{
    public List<Image> Images { get; set; }
}

public class Image
{
    public string Src { get; set; }
    public string Value { get; set; }
}
  • I have no power over the xml feed: it is as it is. I would avoid a custom parser if RestSharp has a way to support this structure. – Diego Ferri Oct 01 '15 at 09:41
  • By the way the RestSharp documentation covers only InlineLists and NestedLists. I see no mention of sequences. – Diego Ferri Oct 01 '15 at 09:48