使用XmlSerializer反序列化复杂类型元素为null

时间:2010-12-27 17:44:11

标签: 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"
            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>

以及以下课程:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_envio_v03.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_envio_v03.xsd", IsNullable = false)]
public partial class ConsultarSituacaoLoteRpsEnvio
{
    [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
    public tcIdentificacaoPrestador Prestador { get; set; }

    [System.Xml.Serialization.XmlElementAttribute(Order = 1)]
    public string Protocolo { get; set; }
}

使用以下代码反序列化对象:

XmlSerializer respSerializer = new XmlSerializer(typeof(ConsultarSituacaoLoteRpsResposta));
StringReader reader = new StringReader(resp);
ConsultarSituacaoLoteRpsResposta respModel = (ConsultarSituacaoLoteRpsResposta)respSerializer.Deserialize(reader);

没有出现任何错误,但对象的属性为null,任何人都知道发生了什么?

2 个答案:

答案 0 :(得分:1)

ConsultarSituacaoLoteRpsResposta!= ConsultarSituacaoLoteRpsEnvio

你可以使用更简单的名字,很难发现:)

您还应该使用XML验证(XmlReaderSettings支持设置)以立即识别问题。并且至少要确保代码是直接从XSD生成的,因为这里存在不匹配。

答案 1 :(得分:1)

好吧,我认为你可能只是忽略了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">

请参阅xmlns="http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_resposta_v03.xsd"属性?这定义了一个默认的XML命名空间。

因此,当您对数据进行反序列化时,您还应该将该默认XML命名空间提供给反序列化器:

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);

这是否适用于包含XML默认命名空间?