XML Serialization, how to do

Asked

Viewed 771 times

1

I need to generate XML in the TISS standard of the ANS. I got the schemas available on website, and by xsd2Code generated tissV3_03_02 classes.

I installed an object of the type ctm_guiaLote and now I need to generate an XML of that object.

I’m trying to do it like this:

 XmlSerializer xmlSerializer = new XmlSerializer(typeof(ctm_guiaLote));
 System.IO.Stream stream = new System.IO.FileStream("F:\\GuiaTeste.xml", System.IO.FileMode.Create);
 xmlSerializer.Serialize(stream, lote.getlote());
 stream.Close();

and I’m having the following exception, among others:

Exception:Thrown: "The TISS type.Itemchoicetype1 does not have the enumeration value 'Item' for the space 'Item' element." (System.Invalidoperationexception) A System.Invalidoperationexception was thrown: "The TISS type.Itemchoicetype1 does not have the enumeration value 'Item' for the space 'Item' element ''." Time: 31/07/2017 12:21:03 Thread:[4416]

And several others like that:

"Error reflecting Type / Property"

Exception:Thrown: "Error reflecting 'Item' property." (System.Invalidoperationexception) A System.Invalidoperationexception was thrown: "Error when reflecting 'Item' property." Time: 31/07/2017 12:21:03 Thread:[4416]

Class ctm_guiaLote:

public partial class ctm_guiaLote
{

    private string numeroLoteField;

    private ctm_guiaLoteGuiasTISS guiasTISSField;

    public ctm_guiaLote()
    {
        this.guiasTISSField = new ctm_guiaLoteGuiasTISS();
    }

    public string numeroLote
    {
        get
        {
            return this.numeroLoteField;
        }
        set
        {
            this.numeroLoteField = value;
        }
    }

    public ctm_guiaLoteGuiasTISS guiasTISS
    {
        get
        {
            return this.guiasTISSField;
        }
        set
        {
            this.guiasTISSField = value;
        }
    }
}

ctm_guidanceGuidesTISS class:

[XmlInclude(typeof(ctm_spsadtGuia))]
public partial class ctm_guiaLoteGuiasTISS
{

    private List<object> itemsField;

    public ctm_guiaLoteGuiasTISS()
    {
        this.itemsField = new List<object>();
    }

    public List<object> Items
    {
        get
        {
            return this.itemsField;
        }
        set
        {
            this.itemsField = value;
        }
    }
}

Code getLote():

    public ctm_guiaLote getlote()
    {
        ctm_guiaLote lote = new ctm_guiaLote();
        lote.numeroLote = "1";
        lote.guiasTISS = new ctm_guiaLoteGuiasTISS();

        List<object> guias = new List<object>();

        ctm_spsadtGuia guia = new ctm_spsadtGuia();
        guia.cabecalhoGuia.registroANS = "1";
        guia.cabecalhoGuia.numeroGuiaPrestador = "1";

        guia.dadosAtendimento.indicacaoAcidente = dm_indicadorAcidente.Item0;
        guia.dadosAtendimento.tipoConsulta = dm_tipoConsulta.Item1;

        guia.dadosSolicitacao.caraterAtendimento = dm_caraterAtendimento.Item1;
        guia.dadosSolicitacao.dataSolicitacao = DateTime.Now;
        guia.dadosSolicitacao.indicacaoClinica = "Teste";

        guia.dadosExecutante.CNES = "123";
        guia.dadosExecutante.contratadoExecutante.ItemElementName = ItemChoiceType1.cnpjContratado;
        guia.dadosExecutante.contratadoExecutante.Item = "11111111111111";
        guia.dadosExecutante.contratadoExecutante.nomeContratado = "Contratado";

        guia.dadosSolicitante.contratadoSolicitante.nomeContratado = "Solicitante";
        guia.dadosSolicitante.contratadoSolicitante.ItemElementName = ItemChoiceType1.cpfContratado;
        guia.dadosSolicitante.contratadoSolicitante.Item = "11111111111";

        guia.dadosAutorizacao.dataAutorizacao = DateTime.Now;
        guia.dadosAutorizacao.numeroGuiaOperadora = "2";
        guia.dadosAutorizacao.senha = "54321";

        guia.dadosBeneficiario.atendimentoRN = dm_simNao.N;
        guia.dadosBeneficiario.nomeBeneficiario = "Paciente";
        guia.dadosBeneficiario.numeroCarteira = "9999";
        guia.dadosBeneficiario.numeroCNS = "12345678901234";

        guia.procedimentosExecutados.Add(new ct_procedimentoExecutadoSadt() { dataExecucao = DateTime.Now, procedimento = new ct_procedimentoDados() { codigoProcedimento = "1", codigoTabela= dm_tabela.Item22, descricaoProcedimento = "Consulta" } });

        guias.Add(guia);

        lote.guiasTISS.Items = guias;

        return lote;
    }

Anyone who can help with these exceptions, thank you very much.

  • What is the code of lote.getlote() ?

  • @Renan only returns an object ctm_guiaLote with some test data filled, I added it also to the question

1 answer

1


I believe I’ve solved the problem:

I deleted the class generated by xsd2Code and remade by command line, using the tool xsd.exe with the following command:

C:\PROGRA~2\MICROS~2\Windows\v7.0A\Bin>xsd /c D:\SchemasTiss\xmldsig-core-schema.xsd D:\SchemasTiss\tissAssinaturaDigital_v1.01.xsd D:\SchemasTiss\tissV3_03_02.xsd  /o:d:\schemastiss\

Where:

D: Schemastiss Directory where XSD was located

/c parameter to generate classes

/o: specify output directory

Schemes:

D: Schemastiss xmldsig-core-schema.xsd

D: Schemastiss tissAssinaturaDigital_v1.01.xsd

D: Schemastiss tissV3_03_02.xsd

The rest of the code remained the same and I managed to generate the xml correctly.

Browser other questions tagged

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