Do you read XML Nfe?

Asked

Viewed 7,973 times

12

I’m doing an XML reading of Nfe however, at the moment I’m reading and playing in a txt, but I can’t read those tags down below:

<?xml version="1.0" encoding="UTF-8"?>
<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="2.00">
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
<infNFe Id="NFe0000000000000000000000000000000000000000" versao="2.00>

I can’t identify those openings <?xml and ?>, and nfeProc and infNFe return me the Node emptiness.

switch (Ler_Xml.NodeType)            
{   
    case XmlNodeType.Element:
        txt_display_xml_envio.AppendText("<" + Ler_Xml.Name);
        txt_display_xml_envio.AppendText(">");
        break;
    case XmlNodeType.ProcessingInstruction:
        txt_display_xml_envio.AppendText("<?" + Ler_Xml.Name);
        txt_display_xml_envio.AppendText("?>");
        break;
    case XmlNodeType.Text:
        txt_display_xml_envio.AppendText(Ler_Xml.Value);
        break;
    case XmlNodeType.EndElement:
        txt_display_xml_envio.AppendText("</" + Ler_Xml.Name);
        txt_display_xml_envio.AppendText( ">");
        txt_display_xml_envio.AppendText(System.Environment.NewLine.ToString());
        txt_display_xml_envio.Update();
        break;
}
  • try to use serialization, http://msdn.microsoft.com/pt-br/library/ms233843.aspx, tonight I make an example with Nfe XML.

  • blz cara vlw, it will be extremely useful to serialize when I’m assembling Nfe, but I’m trying to read an Arq.xml and show it in a test-only textbox, this reading normal and showing, however these above tags I specified in the post are not shown, they return empty like this in the textbox <nfeProc><Nfe><infNFe>, the encoding that comes first of all well is presented.

  • would be like the code snippet that reads XML and loads into the textbox?

  • edited the post, pasted up there the Cod. blz

4 answers

11

The best way to work with Nfe is to use the serialization and deserialization of objects. But in order to deserializar the Xmls of an Nfe, we first need to create the classes according to the XML schemas, which can be downloaded in the portal itself NFE. After downloading, simply generate the classes with the following command

"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\xsd.exe" /c /edb /l:CS /n:PL_008d xmldsig-core-schema_v1.01.xsd consReciNFe_v3.10.xsd consSitNFe_v3.10.xsd consStatServ_v3.10.xsd enviNFe_v3.10.xsd inutNFe_v3.10.xsd leiauteConsSitNFe_v3.10.xsd leiauteConsStatServ_v3.10.xsd leiauteInutNFe_v3.10.xsd leiauteNFe_v3.10.xsd procInutNFe_v3.10.xsd procNFe_v3.10.xsd retConsReciNFe_v3.10.xsd retConsSitNFe_v3.10.xsd retConsStatServ_v3.10.xsd retEnviNFe_v3.10.xsd retInutNFe_v3.10.xsd tiposBasico_v3.10.xsd ./nfe_v3.10.xsd

This will generate a file nfe_v3_10.cs that should be added to your project.

Once this is done, we will be able to deserialize an XML in an object.

To deserialize an XML, just use the Xmlserializer class as the following excerpt.

TEnviNFe myObject;
XmlSerializer mySerializer = new XmlSerializer(typeof(TEnviNFe));
FileStream myFileStream = new FileStream("nfe.xml", FileMode.Open);
myObject = (TEnviNFe)mySerializer.Deserialize(myFileStream);

To find out which class to use in cast of command Deserialize which you will use when deserializar, just look at the first tag of your XML, in this case I used an example of a note sending XML that starts with the tag enviNFe

  • Important detail: in the last file put . before the name, for example .\nfe_v4_00.xsd. Due to a bug in xsd this will make the final archive nfe_v4_00.cs. Fountain: MSDN

9


I may not have quite understood your question, but if you’re wearing one XmlTextReader, then there’s no way you can just want to interpret the first line.

It is a header line that identifies a file as XML and tells which enconding the reader should use to interpret the characters in the file.

Forget that line and go on.


According to your comments, I think I understand your question better. You are reading the XML nodes, in this case they are elements. The elements are the ones that start an entry in XML. Already the attributes extend the elements, example:

 <Elemento1 atributo1="texto do atributo" atributo2="12345">
     <Elemento2>Texto do elemento</Elemento2>
 </Elemento1>

With this, you need to iterate over each node to get the attributes:

foreach (XmlNode item in node.ChildNodes)
{ 
    // Informações do nó (elemento)

    foreach (XmlAttribute att in item.Attributes)
    {
        // informações do atributo
    }
 }
  • assim certo, o encoding da para esquecer de boa rsrsrs depois só criar denovo o problela é nfeProc e infNFe, elas me retornam <nfeProc> e <infNFe> sendo que deveriam estar assim,<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="2.00"><NFe xmlns="http://www.portalfiscal.inf.br/nfe"><infNFe Id="Nfe0000000000000000000000000000" versao="2.00>

  • @Brunopardim updated my answer. See if you can understand well. But as already commented in the other questions, I also use microsoft XSD to generate classes automatically for reading and writing XML.

  • 1

    Exactly man, great help ! That’s exactly it.

5

  • 1

    this is exactly the way, use xsd.exe to generate the classes based on the schemas of the recipe, then just seralize and deserializar the objects

  • @pedroluz I downloaded the XML schematics from the recipe website in the following link . 20 files came. I need to pass all of them as parameter to the xsd.exe command ? I tried to do this, but it tries to generate a file whose name is the name of all 20 concatenated files, so it returns the error that the file name is too big and cannot be generated.

  • 1

    @Rafael, really, what did I do is rename them all to small names, like 1.xsd, 2.xsd and so on and call these names.

  • 1

    @Rafael, in the last file put . before the name, for example .\nfe_v4_00.xsd. Due to a bug in xsd this will make the final archive nfe_v4_00.cs. Fountain: MSDN

-1

As for the xsd name, remove from the command posted in the class generation the command that creates the name /n:Pl_008d. Follows the version Nfe 4.00

No need to change filename.

I copied xsd.exe to the same folder where the xsd are.

xsd /c /edb /l:CS xmldsig-core-schema_v1.01.xsd consReciNFe_v4.00.xsd consSitNFe_v4.00.xsd consStatServ_v4.00.xsd enviNFe_v4.00.xsd inutNFe_v4.00.xsd leiauteConsSitNFe_v4.00.xsd leiauteConsStatServ_v4.00.xsd leiauteInutNFe_v4.00.xsd leiauteNFproce_v4.00.xsd procInutNF_v4.00.xsd.xsd NFe_v4.00.xsd.xsd.Fe_v4.00.xsd retConsSitNFe_v4.00.xsd retConsStatServ_v4.00.xsd retEnviNFe_v4.00.xsd retInutNFe_v4.00.xsd tipsBasico_v4.00.xsd . /nfe_v4.00.xsd

Browser other questions tagged

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