سؤال

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?

هل كانت مفيدة؟

المحلول

Well, I think you might just be ignoring the XML namespace and that might be your problem. In your XSD, you define a default XML namespace:

<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? That defines a default XML namespace.

So when you go to deserialize your data, you should also supply that default XML namespace to the deserializer:

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?

نصائح أخرى

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. And at least make sure that the code was generated from the XSD directly, because there are mismatches here.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top