[英]Using XmlSerializer to deserialize complex type elements with choice
I receive a XML that I'm having problem to deserialize, I cant make a class that fits the XML's schema. 我收到反序列化有问题的XML,无法创建适合XML模式的类。
XML Schema (every type name beginning with "ts" is SimpleType): XML模式(每个以“ ts”开头的类型名称均为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 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 或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. 问题是在2个元素的序列和一个元素之间进行选择。 I can make a choice of elements ok, but the choise of 2 elements and one element no.
我可以选择元素,但是选择2个元素和选择1个元素就没有。
How can I make a class to deserialize this schema? 如何创建一个类以反序列化此模式?
This should work. 这应该工作。 if
ListaMensagemRetorno.Count>0
then this means you have deserialized xml2 else xml1 如果
ListaMensagemRetorno.Count>0
则表示您已反序列化xml2,否则已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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.