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
.