Personal beauty, I'll post an example of serialization, with this feature I can quickly and effortlessly mount my xmls.
A partial class I put together, from the layout of an NFe
Amount using System.Xml.Serialization;
In this example, you can see that in each class, you have properties, properties that can be tags, or groups of tags in xml.
Ex: Below is the root tag of the xml class NFe, inside it we have a child group called infNFe, and inside it we have several tags, which however are classes (which will be groups in xml) or properties no xml). We can define attributes for groups or elements, as well as 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 mount our xml, we have to instantiate class face.
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 ready class and its instance is performed, 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 ns = new XmlSerializerNamespaces ();
ns.Add ("", " link ");
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 content 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>