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
– Leandro Angelo
Ta wrong formatting this XML! is the first problem! error. check this.
– novic
Just for testing, try
public List<string> Codigo { get; set; }
public List<string> Descricao { get; set; }
– Leandro Angelo
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
– Marcos R. Weimer