How to envelop the sending batch of NFe in the header using C #?

0

Hello, I'm not experienced with WebServices , but I'm trying to develop a NFCe sender and I've already been able to authorize some notes in the certification environment. I generate XML using C# , so I validate, sign and validate again comparing with xsd of sefaz , however when I sent the files I had to create the header in notepad++ and then load using XmlDocument to be able to send, because I could not add the Shipping batch in the <nfeDadosMsg></nfeDadosMsg> element.

In summary, I need to insert a xml after an element of another xml The code I use is

public String CabecalhoEnvio(string cUF, int ambiente, String notaAssinada, String URlSoapAction)
{
    String retorno = String.Empty;
    MemoryStream stream = new MemoryStream(); // The writer closes this for us

    using (XmlTextWriter xml = new XmlTextWriter(stream, Encoding.UTF8))
    {
        xml.WriteStartDocument();
        xml.WriteStartElement("soap:Envelope");
        xml.WriteAttributeString("xmlns:soap", "http://www.w3.org/2003/05/soap-envelope");
        xml.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        xml.WriteAttributeString("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
        xml.WriteStartElement("soap:Header");
        xml.WriteStartElement("nfeCabecMsg");
        xml.WriteAttributeString("xmlns", URlSoapAction);
        xml.WriteElementString("cUF", cUF);
        xml.WriteElementString("versaoDados", "3.10");
        xml.WriteEndElement();
        xml.WriteEndElement();
        xml.WriteStartElement("soap:Body");
        xml.WriteStartElement("nfeDadosMsg", URlSoapAction);
        /*inserir o lote assinado aqui sem bugar o XML*/
        xml.WriteEndElement();
        xml.WriteEndElement();
        xml.WriteEndElement();
        xml.WriteEndDocument();
        xml.Flush();
        xml.Flush();

        StreamReader ler = new StreamReader(stream, Encoding.UTF8, true);
        stream.Seek(0, SeekOrigin.Begin);

        retorno += ler.ReadToEnd();
    }
   return retorno;
}

This code generates the following xml:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Header>
    <nfeCabecMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NfeAutorizacao">
      <cUF>11</cUF>
      <versaoDados>3.10</versaoDados>
    </nfeCabecMsg>
  </soap:Header>
  <soap:Body>
    <nfeDadosMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NfeAutorizacao">
    adicionar o lote aqui;
    </nfeDadosMsg>
  </soap:Body>
</soap:Envelope>

I always insert the XmlDocument into the nfeDadosMsg element and the file gets full of strange characters type ?&??&& . I use UTF-8 encoding and what I need to do is look like an include of php .

    
asked by anonymous 10.10.2016 / 06:27

2 answers

0

I decided to treat everything as string and concatenating instead of inserting the batch as a new node in the Soap Ex message: Soap message I split in two and concatenated the batch string.

String cabec1= "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns...><nfeDadosMsg xmlns=\"http://www.portalfiscal.inf.br/nfe/wsdl/NfeAutorizacao\">";String cabec2 = "</nfeDadosMsg></soap:Body</soap:Envelope>";String loteSemDeclaracao = loteAssinado.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");String retorno = cabec1+loteSemDeclaracao+cabec2;

The CDATA option did not work because the QRCode tag in the xml batch is already a CDATA and it is annoying to concatenate CDATAS as J. Guilherme said. And at first Replace was not working because the Visual Studio code formatting adds spaces in "utf - 8" I removed the spaces leaving "utf - 8" and it worked.

    
12.10.2016 / 15:40
-1

You can use CDATA:

<![CDATA[ *texto aqui* ]]>

In XMLTextWrite the writeCData(*xml aqui*) method should be what you are looking for.

    
10.10.2016 / 06:50