Map class for deserialization of XML to C #

0

I need to map this XML into a C # class to deserialize via RestSharp Deserialization :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rsp stat="ok">
<items total="1232177" items="100">
    <media id="4779808" thumb="http://mh-2-rest.panthermedia.net/media/previews/0004000000/04779000/04779808_thumb_170.jpg"/>
    <media id="8950240" thumb="http://mh-2-rest.panthermedia.net/media/previews/0008000000/08950000/08950240_thumb_170.jpg"/>
    <media id="12842738" thumb="http://mh-1-rest.panthermedia.net/media/previews/0012000000/12842000/12842738_thumb_170.jpg"/>
</items>
</rsp>

The class I mapped looks like this:

namespace PantherMediaAPI
{
    public class Media
    {
        public string Id { get; set; }
        public string Thumb { get; set; }
    }

    public class Items
    {
        public List<Media> Media { get; set; }
        public string Total { get; set; }
        public string _items { get; set; }

        public Items()
        {
            this.Media = new List<PantherMediaAPI.Media>(); 
        }
    }

    public class Rsp
    {
        public Items Items { get; set; }
        public string Stat { get; set; }
    }

}

But when I get the answer all attributes are populated minus the item public List<Media> Media { get; set; } :

What am I missing? I have a lot of difficulty with XML because I work more with JSON.

    
asked by anonymous 26.07.2018 / 20:37

1 answer

0

I think that in order to do Deserialize it is necessary that classes be correctly configured with tags XML.

In the example below I put, I think (did not test) all the necessary settings to work properly:

[XmlType("media")]
public class Media
{
    [XmlAttribute("id")]
    public string Id { get; set; }
    [XmlAttribute("thumb")]
    public string Thumb { get; set; }
}

[XmlType("items")]
public class Items
{
    [XmlElement, XmlArray("media")]
    public List<Media> Media { get; set; }
    [XmlAttribute("total")]
    public string Total { get; set; }
    [XmlAttribute("items")]
    public string _items { get; set; }

    public Items()
    {
        this.Media = new List<Media>();
    }
}

[XmlType("rsp")]
public class Rsp
{
    [XmlElement("items")]
    public Items Items { get; set; }
    [XmlAttribute("stat")]
    public string Stat { get; set; }
}

I have only doubts regarding the class Items , which has an element also with name items , I do not know if it will not give problems when constructing the object.

    
27.07.2018 / 10:32