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.