Class structure for

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 deserializing

    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:

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

just load the first error / description, and for the others I have errors of column unknow code / description

See how the class is: (without the part of the header that for contractual reasons I can not expose)

[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; }
    }
}

I've tried changing (XmlElement, XmlArray, XmlArrayItem), searched for more than 2 hours on the internet without success, I must be using the wrong tags in the search.

    
asked by anonymous 13.03.2018 / 15:17

1 answer

0

Since 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 may 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 pair 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 guarantee that.

    
13.03.2018 / 17:17