0

我有以下架构:

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

没有发生任何错误,但对象的属性为空,有人知道发生了什么吗?

4

2 回答 2

1

顾问SituacaoLoteRpsResposta != 顾问SituacaoLoteRpsEnvio

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

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

于 2010-12-27T17:51:32.857 回答
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 默认命名空间?

于 2010-12-27T18:19:48.133 回答