Namespace Serialization with link

Asked

Viewed 1,002 times

1

I’m working with Serialization, I’m adding Namespace to two tags, nfeProc and Nfe. I can normally add the following:

[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 to Child Nfe, it doesn’t work with the same link:

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

Upshot:

<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 -_-
  • 1

    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 to versao "2.00" was disabled this month, now is 3.10

  • 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.

  • rsrsrsr got it, rsrsrsrsrs, this as xmls were small I used a stringbuilder to generate rsrsrsrsrsr, now yes I am using serialization Rsrsrsrs.

  • I can’t repeat the namespace.

3 answers

1

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>

0

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

I chose to use Serialization, from a structured class Gero quickly and dynamically an xml.

https://msdn.microsoft.com/pt-br/library/ms233843.aspx

  • Can you also post a code example about how your serialization turned out? The goal is to help more users in the future. Thank you!

  • blz, no problem. I’ll post

0

This what 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 namespace.

The exception occurs when there are two definitions of namespace in the parent element, for example:

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

Then you’d be setting two namespaces possible by identifying with a prefix to which namespace the child element belongs:

<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 -->
  • 1

    Blz dude, I’m already Serializing the xml completely by a class, good responsta tbm.

Browser other questions tagged

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