简体   繁体   English

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

[英]Using XmlSerializer deserialize complex type elements are null

I have the following schema: 我有以下架构:

<?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>

and the following class: 和以下课程:

[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; }
}

Use the following code to deserialize the object: 使用以下代码反序列化对象:

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

does not occur any error but the properties of objects are null, anyone know what is happening? 不会发生任何错误,但对象的属性为null,任何人都知道发生了什么?

ConsultarSituacaoLoteRpsResposta != ConsultarSituacaoLoteRpsEnvio ConsultarSituacaoLoteRpsResposta!= ConsultarSituacaoLoteRpsEnvio

You could have used easier names, it was hard to spot :) 你可以使用更简单的名字,很难发现:)

You should also use XML validation (XmlReaderSettings supports setting it up) to instantly identify the problems. 您还应该使用XML验证(XmlReaderSettings支持设置)以立即识别问题。 And at least make sure that the code was generated from the XSD directly, because there are mismatches here. 并且至少要确保代码是直接从XSD生成的,因为这里存在不匹配。

Well, I think you might just be ignoring the XML namespace and that might be your problem. 好吧,我认为你可能只是忽略了XML命名空间,这可能是你的问题。 In your XSD, you define a default XML namespace: 在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">

See the xmlns="http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_resposta_v03.xsd" attribute? 请参阅xmlns="http://www.ginfes.com.br/servico_consultar_situacao_lote_rps_resposta_v03.xsd"属性? That defines a default XML namespace. 定义了默认的XML名称空间。

So when you go to deserialize your data, you should also supply that default XML namespace to the deserializer: 因此,当您要对数据进行反序列化时,还应该向反序列化器提供默认的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);

Does that work with the inclusion of the XML default namespace? 这是否适用于包含XML默认命名空间?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM