deserialize xml c#

Asked

Viewed 408 times

3

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

xml:

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

taken 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 don’t fall for any Ecstasy, you’re doing everything backwards. I just need to know how to redeem the values of the classes, display them or save them in the bank, for example

  • 1

    The Xmltype class is correct? It is "rstult", it should not be "result"?

  • Include this information by editing your original question and delete this.

  • Possible duplicate of deserialize xml

  • In addition to the misstype you are also not Rializando the elmento <entries>and its type <entry>

  • I fixed the error of the result, the Ntries I didn’t want to serialize anyway

  • [Xmlelement("Entries")] public Object Entries { get; set; } I’ve added it now

  • I don’t get any errors. I wanted to be able to visualize the content deserialized

  • Tried to put a breakpoint?

Show 3 more comments

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 the XmlType of the solved "result" and entries entries and entry added.
If you do not want to get information from entries put the XmlIgnore.

  • I made these changes, it’s all right... as I see the information deserialized?

  • Supposedly the variable Testing has the object with the information.

  • Got it!! thank you so much. Now I’m trying to get the alternativeidentifier, How do I access the item from the created list? [Xmltype("Schedule")] public class Schedule { [Xmlelement("alternativeIdentifier")] public string alternativeIdentifier { get; set; } }

  • Perhaps it would be better to edit your question and put there this new structure, because this Schedule is not in the initial XML definition.

Browser other questions tagged

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