Personal beauty, I will post an example of serialization, with this feature I can mount quickly and without complication my xmls.
A partial class I assembled from the layout of an Nfe
import using System.xml.Serialization;
In this example, you can see that in each class, it has properties, properties that can become tags, or tag groups in xml.
Ex: Below we have the xml root tag the Nfe class, enter it we have a child group called infNFe, and within it we have several tags, but be classes (which will be groups in xml) or properties (which will be elements in xml). We can define attributes for the groups or elements, as well as the order
//adicionando namespace na tag root
[XmlRoot("NFe", Namespace = "http://www.portalfiscal.inf.br/nfe")]
public class NFe // tag root no xml
{
public infNFe infNFe; // tag grupo filho do root
}
public class infNFe
{
[XmlAttribute("Id")]
public string Id { get; set; } //essa propriedade é um atributo, neste grupo infNFE, definifo por [XmlAttribute("Id")] acima dele
[XmlAttribute("versao")]
public string versao { get; set; } //a mesma explicação acima
public ide ide; //tag grupo filho do infNFE
public emit emit; //tag grupo filho do infNFE
}
public class ide
{
[XmlElement(Order = 1)] // esse elemento vai ser o primeiro dentro do grupo ide
public string cUF { get; set; }
public string cNF { get; set; }
public string natOp { get; set; }
public string indPag { get; set; }
public string mod { get; set; }
public string serie { get; set; }
public string nNF { get; set; }
}
public class emit{
//grupo vazio
}
After the mounted class, to be able to assemble our xml, we have with instantiation expensive class.
Ex:
NFe serial_exemplo = new NFe(); //Instanciando o root do xml
serial_exemplo.infNFe = new(); //Depois de instanciar NFe temos acesso a infNFe e instanciamos.
serial_exemplo.infNFe.Id= "teste"; // setando uma propriedade definida com item na classe ex no xml: <infNFe Id="teste"></infNFe>
serial_exemplo.infNFe.versao = "teste";
serial_exemplo.infNFe.ide = new ide(); //tag ide grupo, sua classe tem propriedades para setar(elementos)
serial_exemplo.infNFe.emit = new emit(); //tag emit grupo
//propriedades presentes na classe ide que serão elementos no xml.
serial_exemplo.infNFe.ide.cUF = "TESTE";
serial_exemplo.infNFe.ide.cNF = "TESTE";
serial_exemplo.infNFe.ide.natOp = "TESTE";
serial_exemplo.infNFe.ide.indPag = "TESTE";
serial_exemplo.infNFe.ide.mod = "TESTE";
serial_exemplo.infNFe.ide.serie = "TESTE";
serial_exemplo.infNFe.ide.nNF = "TESTE";
After the class is ready and completed, it is necessary to serialize the class and generate the file.
var xml_string = new System.Text.StringBuilder();// crio uma var string
var internalWriter = new System.IO.StringWriter(xml_string); // refenciado a minha stringbuilder para uma StringWriter.
System.Xml.XmlWriter writer = new System.Xml.XmlTextWriter(internalWriter); // referencia da StringWriter para writer porém do Writer.
System.Xml.Serialization.XmlSerializer serializer = new
System.Xml.Serialization.XmlSerializer(typeof(NFe)); // referencia a classe para o serial
//if a custom namespace ex:
Xmlserializernamespaces = new Xmlserializernamespaces();
ns Add.("", "http://www.portalfiscal.inf.br/nfe");
serializer.Serialize(writer, nfe, ns);// por fim, cria o xml(writer), com base da classe(nfe), namespace no grupo filho da tag root.
Now you can create the file with the contents of xml_string.
The end of the file will be this:
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
<infNFe Id="teste" versao="teste" xmlns="http://www.portalfiscal.inf.br/nfe">
<ide>
<cUF>teste</cUF>
<cNF>teste</cNF>
<natOp>teste</natOp>
<indPag>teste</indPag>
<mod>teste</mod>
<serie>teste</serie>
<nNF>teste<nNF>
</ide>
<emit></emit>
</infNFe>
</NFe>
Why is the wheel reinvented? : ) http://answall.com/questions/44130/leitura-xml-nfe?rq=1
O melhor caminho para trabalhar com NFe é usar a serialização e deserialização de objetos.
Another tip toversao "2.00"
was disabled this month, now is3.10
– rubStackOverflow
As I said, I’m using serialization, as for the version, I’m performing tests generating xml, I’m not instantiating web service, the problem I have is that I can’t put a namespace in the said tag.
– bp002
rsrsrsr got it, rsrsrsrsrs, this as xmls were small I used a stringbuilder to generate rsrsrsrsrsr, now yes I am using serialization Rsrsrsrs.
– bp002
I can’t repeat the namespace.
– bp002