desserializar xml c#

Question:

I made a call through my web api and put it in a class. How do I display 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 the 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 don't fall for any exceptions, you are doing everything correctly. I just need to know how to retrieve the values ​​of classes, display them or save them in the bank, for example

Answer:

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 the XmlType of "result" resolved and entries and entry added.
If you eventually don't want to get information from the entries put XmlIgnore .

Scroll to Top
AllEscort