Serialization Namespace with link

1

I'm working with Serialization, I'm adding Namespace to two tags, A nfeProc and NFe. I can usually add the following Name:

[XmlRoot("nfeProc", Namespace = "http://www.portalfiscal.inf.br/nfe")]

In XML:

<nfeProc versao="2.00" xmlns="http://www.portalfiscal.inf.br/nfe">

But when I try to add the same namespace in child NFe, it does not work with the same link:

XmlElement("NFe", Namespace = "http://www.portalfiscal.inf.br/nfe")]
 public NFe NFe;

Result:

<nfeProc versao="2.00" xmlns="http://www.portalfiscal.inf.br/nfe">
  <NFe>

If I change the link, put a space before, then I will any character appears:

<nfeProc versao="2.00" xmlns="http://www.portalfiscal.inf.br/nfe">
  <NFe xmlns="http://www.portalfiscal.inf.br/nfe ">
                                              **^**
                                             espaço -_-
    
asked by anonymous 23.04.2015 / 22:31

3 answers

1

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>
    
04.01.2016 / 11:15
0

This you are trying to do is not only incorrect as is out of standard . If there is xmlns defined in the parent element, all children automatically belong to the namespace .

The exception occurs when there are two namespace definitions in the parent element, such as:

<nfeProc versao="2.00" xmlns="http://www.portalfiscal.inf.br/nfe" temp:xmlns="http://tempuri.org">

Here you would be defining two possible namespaces , identifying with a prefix which namespace the child element belongs to:

<NFe xmlns="http://www.portalfiscal.inf.br/nfe "></NFe><!-- Usa o namespace http://www.portalfiscal.inf.br/nfe -->
<temp:exemplo><temp:exemplo><!-- Usa o namespace http://tempuri.org -->
    
30.12.2015 / 20:00
0

The best way to work with NFe is to use serialization and deserialization of objects. Another tip the "2.00" version has been disabled this month, it is now 3.10 @rubStackOverflow

I have chosen to use Serialization, from a structured class, I quickly and dynamically generate an xml.

  

link

    
02.01.2016 / 12:16