deserialize xml c #

3

I made a call through my web api and put it in a class. How do I display the values?

xml:

<result>
<resourceName>activity</resourceName>
<size>1</size>
<entries>
<entry id="1802274" link="/activity/1802274.xml"/>
</entries>
</result>

I get the xml:

public class PegaVisita
    {

        HttpClient client;
        Uri usuarioUri;

        public async System.Threading.Tasks.Task GeraLocalAsync()
        {
            try
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("https://api.umov.me");
                    HttpResponseMessage response = client.GetAsync("CenterWeb/api/meutoken/activity.xml?description=Cancelar&Coletas").Result;

                    string tarefa = await response.Content.ReadAsStringAsync();


                    if (response.IsSuccessStatusCode)
                    {
                        var buffer = Encoding.UTF8.GetBytes(tarefa);
                        using (var stream = new MemoryStream(buffer))
                        {
                            var serializer = new XmlSerializer(typeof(Result));
                            var Teste = (Result)serializer.Deserialize(stream);
                        }


                        usuarioUri = response.Headers.Location;
                    }
                }
            }
            catch (Exception ex)
            {

                Console.Write("Erro ao cadastrar os locais");
                Console.Write("Error : " + ex.Message);
            }
        }
    }

class that receives xml

namespace WsCliMotoristas
{
    [Serializable]
    [XmlRoot("result"), XmlType("rstult")]
    public class Result
    {
        [XmlElement("resourceName")]
        public string resourceName { get; set; }
        [XmlElement("size")]
        public string size { get; set; }


    }
}

I do not fall into any exception, you're doing everything right. I just need to know how to retrieve the values of the classes, display them or save them in the database, for example

    
asked by anonymous 26.07.2018 / 18:23

1 answer

1

Try it this way:

namespace WsCliMotoristas
{
    [Serializable]
    [XmlRoot("result"), XmlType("result")]
    public class Result
    {
        [XmlElement("resourceName")]
        public string resourceName { get; set; }
        [XmlElement("size")]
        public string size { get; set; }
        [XmlArray("entries")]
        public List<entry> entries { get; set; }

        [XmlType("entry")]
        public class entry
        {
            [XmlAttribute("id")]
            public string id { get; set; }
            [XmlAttribute("link")]
            public string link { get; set; }
        }
    }
}

Already with XmlType of "result" solved and entries entries and entry added.
If you do not want to get information from entries put XmlIgnore .

    
26.07.2018 / 18:36