Map class to XML deserialization for C#

Asked

Viewed 252 times

0

I need to map this XML to 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 Mapeed was 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 come populated except the item public List<Media> Media { get; set; }:

inserir a descrição da imagem aqui

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

  • Have you tried using the Visual Studio "XML Paste as Classes"?

  • Anyway, public string _items shouldn’t be public string items?

  • @Ricardopunctual the deserializer understands that _items == items so much so that I used this site:https://xmltocsharp.azurewebsites.net/ and it returned me the same class

  • @Ricardopunctual I will test with XML the Classes ;)

  • #partilGabiarra ... Xmldeserializer xml = new Xmldeserializer(); Rsp output = xml.Deserialize<Rsp>(new Restresponse { Content = text,Contenttype = "application/xml" }); var output2 = xml.Deserialize<List<Media>>(new Restresponse { Content = text,Contenttype = "application/xml" }); output.Items.Media = output2;

1 answer

0

I think to do the Deserialize it is necessary that the classes are correctly configured with the tags XML.

In the example below I put, I think (I have not tested), all the necessary settings for it 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 about the class Items, which has an element also named items, I don’t know if it will cause problems when building the object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.