Class structure for

Asked

Viewed 46 times

1

I have the following XML returned by a service:

<?xml version="1.0" encoding="iso-8859-1" ?>
<mensagem tipo="0000" version="1.0">
  <registro tipo="0000" version="1.0">
    <header>
      <campos que não posso disponibilizar/>
    </header>
    <erros>
      <qtdMensagem>3</qtdMensagem>
      <erro>
        <codigo>B0000</codigo>
        <descricao>corrigir campo x</descricao>
        <codigo>B0000</codigo>
        <descricao>corrigir campo y</descricao>
        <codigo>B0000</codigo>
        <descricao>campo z invalido</descricao>
      </erro>
    </erros>
  </registro>
</mensagem>

I’m having trouble deserializaring

    public void ReadXML<T>(ref T AObject, string AXml)
    {
        Errors.Clear();

        try
        {
            byte[] encodedString = Encoding.UTF8.GetBytes(AXml);
            MemoryStream ms = new MemoryStream(encodedString);
            ms.Flush();
            ms.Position = 0;

            XmlSerializer ser = new XmlSerializer(typeof(T));
            ser.UnknownAttribute += new XmlAttributeEventHandler(Serializer_UnknownAttribute);
            ser.UnknownElement += new XmlElementEventHandler(Serializer_UnknownElement);
            ser.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode);
            ser.UnreferencedObject += new UnreferencedObjectEventHandler(Serializer_UnreferencedObject);

            AObject = (T)ser.Deserialize(ms);
        }
        catch (Exception ex)
        {
            throw new UException(1, ex, "Erro ao serializar.");
        }
    }

calling for:

Msg RetMsg = new Msg();
ReadXML<Msg>(ref RetMsg, XML);

only loads the first error/description, and for the others I have error column unknow code/description

Look at the class: (without the part of the header that I cannot explain for contractual reasons)

[XmlRoot(ElementName = "erros")]
public class ErrosClas
{
    [XmlElement(ElementName = "qtdMensagem")]
    public string QtdMensagem { get; set; }

    [XmlElement(ElementName = "erro")]
    public List<Erro> AErro { get; set; }

    [XmlRoot(ElementName = "erro")]
    public class Erro
    {
        [XmlElement(ElementName = "codigo")]
        public string Codigo { get; set; }

        [XmlElement(ElementName = "descricao")]
        public string Descricao { get; set; }
    }
}

Already tested changing (Xmlelement, Xmlarray, Xmlarrayitem), I searched for more than 2 hours on the internet unsuccessfully, I must be using the wrong tags in the search.

  • as it is the formatting of <error> is somewhat strange, it would represent an array of code and another of Description

  • Ta wrong formatting this XML! is the first problem! error. check this.

  • Just for testing, try public List<string> Codigo { get; set; } public List<string> Descricao { get; set; }

  • XML is from a financial (bank) has no way to change, they insist on things like empty tag must be <tag></tag>. I tested with Leandro’s suggestion

1 answer

0


Once the element <erro> is composed of several <codigo> and <descricao> Your mapping should include lists or arrays for them:

  <erro>
    <codigo>B0000</codigo>
    <descricao>corrigir campo x</descricao>
    <codigo>B0000</codigo>
    <descricao>corrigir campo y</descricao>
    <codigo>B0000</codigo>
    <descricao>campo z invalido</descricao>
  </erro>

Soon your mapping can reflect as follows

[XmlRoot(ElementName = "erros")]
public class ErrosClas
{
    //...

    [XmlRoot(ElementName = "erro")]
    public class Erro
    {
        [XmlElement(ElementName = "codigo")]
        public List<string> Codigo { get; set; }

        [XmlElement(ElementName = "descricao")]
        public List<string> Descricao { get; set; }
    }
}

But when processing the result in your system you will need to match these values (Code/Description) through their indexes, because in this case you will receive two distinct lists that must be paired, but there is no explicit rule that guarantees this.

Browser other questions tagged

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