Problems when deserializing XML

Asked

Viewed 207 times

0

I have the following code:

public T LerXml<T>(string pArquivoXML)
{
    byte[] xmlBytes = Encoding.UTF8.GetBytes(pArquivoXML);
    Stream xmlStream = new MemoryStream(xmlBytes);

    using (var stream = new System.IO.StreamReader(xmlStream))
    {
        XmlSerializer infoSerializer = new XmlSerializer(typeof(T));
        var objeto = infoSerializer.Deserialize(stream);
        stream.Close();
        return (T)objeto;
    }
}

But when trying to deserialize, the following Exception occurs:

when Converting a string to datetime parse the string to take the date before putting each variable into the datetime Object

This Exception occurs exactly in this section:

var objeto = infoSerializer.Deserialize(stream); 

The type I pass contains these Datetime fields where probably making the error occur:

public System.DateTime DataEmissao
{
    get
    {
        return this.dataEmissaoField;
    }
    set
    {
        this.dataEmissaoField = value;
    }
}

public System.DateTime DataEmissaoRps
{
    get
    {
        return this.dataEmissaoRpsField;
    }
    set
    {
        this.dataEmissaoRpsField = value;
    }
}

XML:

<?xml version="1.0" encoding="UTF-8"?>
<ns3:ConsultarLoteRpsResposta xmlns:ns3="http://www.ginfes.com.br/servico_consultar_lote_rps_resposta_v03.xsd" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" xmlns:ns4="http://www.ginfes.com.br/tipos_v03.xsd">
   <ns3:ListaNfse>
      <ns3:CompNfse>
         <ns4:Nfse>
            <ns4:InfNfse>
               <ns4:Numero>8</ns4:Numero>
               <ns4:CodigoVerificacao>000000000</ns4:CodigoVerificacao>
               <ns4:DataEmissao>2017-06-12T15:34:52</ns4:DataEmissao>
               <ns4:IdentificacaoRps>
                  <ns4:Numero>12</ns4:Numero>
                  <ns4:Serie>AA</ns4:Serie>
                  <ns4:Tipo>1</ns4:Tipo>
               </ns4:IdentificacaoRps>
               <ns4:DataEmissaoRps>2017-06-12</ns4:DataEmissaoRps>
               <ns4:NaturezaOperacao>1</ns4:NaturezaOperacao>
               <ns4:RegimeEspecialTributacao>0</ns4:RegimeEspecialTributacao>
               <ns4:OptanteSimplesNacional>2</ns4:OptanteSimplesNacional>
               <ns4:IncentivadorCultural>2</ns4:IncentivadorCultural>
               <ns4:Competencia>201706</ns4:Competencia>
               <ns4:Servico>
                  <ns4:Valores>
                     <ns4:ValorServicos>152</ns4:ValorServicos>
                     <ns4:IssRetido>2</ns4:IssRetido>
                     <ns4:ValorIss>3.04</ns4:ValorIss>
                     <ns4:BaseCalculo>152</ns4:BaseCalculo>
                     <ns4:Aliquota>2</ns4:Aliquota>
                  </ns4:Valores>
                  <ns4:ItemListaServico>107</ns4:ItemListaServico>
                  <ns4:CodigoTributacaoMunicipio>00000000</ns4:CodigoTributacaoMunicipio>
                  <ns4:Discriminacao>Servico em Informatica</ns4:Discriminacao>
                  <ns4:CodigoMunicipio>0000000</ns4:CodigoMunicipio>
               </ns4:Servico>
               <ns4:ValorCredito>0</ns4:ValorCredito>
               <ns4:PrestadorServico>
                  <ns4:IdentificacaoPrestador>
                     <ns4:Cnpj>0000000000000</ns4:Cnpj>
                     <ns4:InscricaoMunicipal>000000</ns4:InscricaoMunicipal>
                  </ns4:IdentificacaoPrestador>
                  <ns4:RazaoSocial>teste</ns4:RazaoSocial>
                  <ns4:Endereco>
                     <ns4:Endereco>teste</ns4:Endereco>
                     <ns4:Numero>S/N</ns4:Numero>
                     <ns4:Complemento>teste</ns4:Complemento>
                     <ns4:Bairro>teste</ns4:Bairro>
                     <ns4:CodigoMunicipio>000000</ns4:CodigoMunicipio>
                     <ns4:Uf>SP</ns4:Uf>
                     <ns4:Cep>000000000</ns4:Cep>
                  </ns4:Endereco>
                  <ns4:Contato>
                     <ns4:Telefone>000000000</ns4:Telefone>
                     <ns4:Email>[email protected]</ns4:Email>
                  </ns4:Contato>
               </ns4:PrestadorServico>
               <ns4:TomadorServico>
                  <ns4:IdentificacaoTomador>
                     <ns4:CpfCnpj>
                        <ns4:Cnpj>00000000000000</ns4:Cnpj>
                     </ns4:CpfCnpj>
                  </ns4:IdentificacaoTomador>
                  <ns4:RazaoSocial>teste</ns4:RazaoSocial>
                  <ns4:Endereco>
                     <ns4:Endereco>teste</ns4:Endereco>
                     <ns4:Numero>000</ns4:Numero>
                     <ns4:Bairro>Centro</ns4:Bairro>
                     <ns4:CodigoMunicipio>000000</ns4:CodigoMunicipio>
                     <ns4:Uf>SP</ns4:Uf>
                     <ns4:Cep>00000000</ns4:Cep>
                  </ns4:Endereco>
                  <ns4:Contato>
                     <ns4:Telefone>0000000000</ns4:Telefone>
                     <ns4:Email>[email protected]</ns4:Email>
                  </ns4:Contato>
               </ns4:TomadorServico>
               <ns4:OrgaoGerador>
                  <ns4:CodigoMunicipio>0000000</ns4:CodigoMunicipio>
                  <ns4:Uf>SP</ns4:Uf>
               </ns4:OrgaoGerador>
            </ns4:InfNfse>
         </ns4:Nfse>
      </ns3:CompNfse>
   </ns3:ListaNfse>
</ns3:ConsultarLoteRpsResposta>
  • You can show example of xml file?

  • Yes, I’ll include it in the question...

  • ready includes xml only hiding personal information

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.