C# 使用XmlSerializer反序列化复杂类型元素为null
我有以下模式:C# 使用XmlSerializer反序列化复杂类型元素为null,c#,.net,xmlserializer,deserialization,C#,.net,Xmlserializer,Deserialization,我有以下模式: <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tipos="http://www.ginfes.com.br/tipos_v03.xsd" targetNamespace="http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_resposta_v03.xsd"
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tipos="http://www.ginfes.com.br/tipos_v03.xsd" targetNamespace="http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_resposta_v03.xsd"
xmlns="http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_resposta_v03.xsd" attributeFormDefault="unqualified" elementFormDefault="qualified">
<xsd:import schemaLocation="tipos_v03.xsd" namespace="http://www.ginfes.com.br/tipos_v03.xsd" />
<xsd:element name="ConsultarSituacaoLoteRpsResposta">
<xsd:complexType>
<xsd:choice>
<xsd:sequence>
<xsd:element name="NumeroLote" type="tipos:tsNumeroLote" minOccurs="1" maxOccurs="1"/>
<xsd:element name="Situacao" type="tipos:tsSituacaoLoteRps" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
<xsd:element ref="tipos:ListaMensagemRetorno" minOccurs="1" maxOccurs="1"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
使用以下代码反序列化对象:
XmlSerializer respSerializer = new XmlSerializer(typeof(ConsultarSituacaoLoteRpsResposta));
StringReader reader = new StringReader(resp);
ConsultarSituacaoLoteRpsResposta respModel = (ConsultarSituacaoLoteRpsResposta)respSerializer.Deserialize(reader);
不会发生任何错误,但对象的属性为空,有人知道发生了什么吗?ConsultarSituacaoLoteRpsResposta!=领事馆 你可以用更简单的名字,但很难辨认:)
您还应该使用XML验证(XmlReaderSettings支持设置)来立即识别问题。至少要确保代码是直接从XSD生成的,因为这里有不匹配的地方。好吧,我认为您可能只是忽略了XML名称空间,这可能是您的问题所在。在XSD中,定义默认的XML命名空间:
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tipos="http://www.ginfes.com.br/tipos_v03.xsd"
targetNamespace="http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_resposta_v03.xsd"
xmlns="http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_resposta_v03.xsd"
attributeFormDefault="unqualified" elementFormDefault="qualified">
这对包含XML默认名称空间有用吗?但他正试图反序列化为
ConsultarSituacaoLoteRpsResposta
类型-我想说这是XML模式中定义的类型,不是吗?嗯,你可能是对的。无论如何,这是一个完全混乱的局面,因为他发布了其他内容的模式。我对模式有控制权,XML只接收和处理,我将尝试删除名称空间,看看它是否有效。谢谢
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tipos="http://www.ginfes.com.br/tipos_v03.xsd"
targetNamespace="http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_resposta_v03.xsd"
xmlns="http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_resposta_v03.xsd"
attributeFormDefault="unqualified" elementFormDefault="qualified">
XmlSerializer respSerializer = new
XmlSerializer(typeof(ConsultarSituacaoLoteRpsResposta),
"http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_resposta_v03.xsd");
StringReader reader = new StringReader(resp);
ConsultarSituacaoLoteRpsResposta respModel =
(ConsultarSituacaoLoteRpsResposta)respSerializer.Deserialize(reader);