Reading XML NFe?

10

I'm doing an XML reading from Nfe however, I'm currently reading and playing in a txt , but I can not read those tags 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 not identify these openings of node <?xml and ?> , and nfeProc and infNFe returns me node empty.

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;
}
    
asked by anonymous 16.12.2014 / 17:05

3 answers

8

I may not have understood your question well, but if you are using a XmlTextReader , then there is no way you can simply interpret the first line.

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

Forget this line and go ahead.

According to your comments, I think I understood your question better. You are reading the XML nodes, in this case, they are elements. The elements are those that get an entry in the XML. Attributes extend the elements, for 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
    }
 }
    
17.12.2014 / 01:07
9

The best way to work with NFe is to use serialization and deserialization of objects. But in order for us to deserialize the XMLs of an NFe, we must first create the classes according to the XML schemas, which can be downloaded from the NFE . After downloading, just 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 nfe_v3_10.cs file that should be added to your project.

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

To deserialize an XML, simply use the XmlSerializer class as follows:

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

To know which class to use in cast of the Deserialize command that you will be using at the time of deserialization, just look at the first tag of your XML, in that case, I used an example of a note sending XML that starts with the tag enviNFe

    
17.12.2014 / 01:00
6

Have you ever thought about using xsd.exe to generate classes from Recipe Schema XSD's?

My whole NFe issue system is based on these classes, it works great.

link

    
16.12.2014 / 23:46