2

I receive a XML that I'm having problem to deserialize, I cant make a class that fits the XML's schema.

XML Schema (every type name beginning with "ts" is SimpleType):

<xsd:element name="ConsultarSituacaoLoteRpsResposta">
    <xsd:complexType>
        <xsd:choice>
            <xsd:sequence>
                <xsd:element name="NumeroLote" type="tsNumeroLote" minOccurs="1" maxOccurs="1"/>
                <xsd:element name="Situacao" type="tsSituacaoLoteRps" minOccurs="1" maxOccurs="1"/>
            </xsd:sequence>
            <xsd:element ref="ListaMensagemRetorno" minOccurs="1" maxOccurs="1"/>
        </xsd:choice>
    </xsd:complexType>
</xsd:element>

<xsd:element name="ListaMensagemRetorno">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="MensagemRetorno" type="tcMensagemRetorno" minOccurs="1" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

<xsd:complexType name="tcMensagemRetorno">
    <xsd:sequence>
        <xsd:element name="Codigo" type="tsCodigoMensagemAlerta" minOccurs="1" maxOccurs="1"/>
        <xsd:element name="Mensagem" type="tsDescricaoMensagemAlerta" minOccurs="1" maxOccurs="1"/>
        <xsd:element name="Correcao" type="tsDescricaoMensagemAlerta" minOccurs="0"/>
    </xsd:sequence>
</xsd:complexType>

I receive:

XML 1

<ConsultarSituacaoLoteRpsResposta xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd">
    <NumeroLote>21</NumeroLote>
    <Situacao>4</Situacao>
</ConsultarSituacaoLoteRpsResposta>

Or XML 2

<ConsultarSituacaoLoteRpsResposta xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd">
   <ListaMensagemRetorno>
      <MensagemRetorno>
           <Codigo>E01</Codigo>
           <Mensagem>AAA</Mensagem>
           <Correcao>BBB</Correcao>
      </MensagemRetorno>
      <MensagemRetorno>
           <Codigo>E02</Codigo>
           <Mensagem>CCC</Mensagem>
           <Correcao>DDD</Correcao>
      </MensagemRetorno>
   </ListaMensagemRetorno>
</ConsultarSituacaoLoteRpsResposta>

The choice between a sequence of 2 elements and an element is the problem. I can make a choice of elements ok, but the choise of 2 elements and one element no.

How can I make a class to deserialize this schema?

Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127
  • 1
    you should look at xsd.exe that will help you generate a class from and xsd or to create an xsd from xml and then a class from the xsd – John Sobolewski Mar 05 '12 at 19:41

1 Answers1

1

This should work. if ListaMensagemRetorno.Count>0 then this means you have deserialized xml2 else xml1

public class ConsultarSituacaoLoteRpsResposta
{
    public int NumeroLote { set; get; }
    public int Situacao { set; get; }
    public List<MensagemRetorno> ListaMensagemRetorno { get; set; }
}
public class MensagemRetorno
{
    public string Codigo { set; get; }
    public string Mensagem { set; get; }
    public string Correcao { set; get; }
}

XmlSerializer serializer = new XmlSerializer(typeof(ConsultarSituacaoLoteRpsResposta), "http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd");
var obj1 = (ConsultarSituacaoLoteRpsResposta)serializer.Deserialize(new StringReader(xml1));
var obj2 = (ConsultarSituacaoLoteRpsResposta)serializer.Deserialize(new StringReader(xml2));
L.B
  • 114,136
  • 19
  • 178
  • 224
  • It's not the answer I was hoping for, but this work around will do for this case. But if I need to serialize this class it will not work, because the schema will not validate the xml if it has 3 elements on it. – LazyLeecher Mar 08 '12 at 14:49