簡體   English   中英

使用XmlSerializer反序列化復雜類型元素為null

[英]Using XmlSerializer deserialize complex type elements are null

我有以下架構:

<?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,任何人都知道發生了什么?

ConsultarSituacaoLoteRpsResposta!= ConsultarSituacaoLoteRpsEnvio

你可以使用更簡單的名字,很難發現:)

您還應該使用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">

請參閱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默認命名空間?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM