How to envelop the Nfe shipment batch in the header using C#?

Asked

Viewed 1,205 times

1

Hello, I have no experience with WebServices, but I’m trying to develop a NFCe and I’ve already been able to authorize some notes in type approval environment. Gero XML using C#, then I validate, sign and validate again comparing with the xsd of sefaz, but when I sent the files I had to create the header on notepad++ and then load using XmlDocument to be able to send, because I could not add the Shipment Batch to the element <nfeDadosMsg></nfeDadosMsg>.

Summarizing 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 in the element nfeDadosMsg the file gets filled with weird characters like ?&??&&. coding use UTF-8 and what I need to do is like a include of php.

2 answers

0


I decided to treat everything as a string and concatenating instead of inserting the batch as a new node of the Soap Ex message: the Soap message I divided in two and concatenated with 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 didn’t work because the Qrcode tag in the xml batch is already a CDATA and it’s boring to concatenate CDATAS like 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.

-1

You can use CDATA:

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

In a XMLTextWrite the method writeCData(*xml aqui*) must be what you seek.

  • It would be more or less that, only I did the test and returned me an Exception It is not possible to have ']]>' inside a CDATA XML block. But it only has the same xml tags

  • when using the method WriteCData(), the method itself who takes care of the syntax. You are putting the XML inside the method WriteCData()?

  • Yes xml.WriteStartElement("nfeDadosMsg", URlSoapAction);&#xA; xml.WriteCData(notaAssinada);&#xA; xml.WriteEndElement();&#xA; xml.WriteEndElement(); the notable variableAssinada is that it has the string containing xml

  • your noteAssinada has CDATA inside her?

  • I was looking here and there’s a Cdata on the Qrcode tag and if I take the Cdata the Qrcode is invalid

  • ah, so it really complicates... concatenate Cdatas is complicated but it works if you change the ]]> for ]]]]><![CDATA[>, with the exception of the last ]]>

  • I was able to divide the header message into two strings and concatenate the batch with the message. But am I getting two statements <?xml version="1.0" encoding="utf-8"? > and wanted to remove one, tried using Replace("<?xml version="1.0" encoding="utf-8"?>"," "); but unsuccessfully.

Show 2 more comments

Browser other questions tagged

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