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
.
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
– RonyElias
when using the method
WriteCData()
, the method itself who takes care of the syntax. You are putting the XML inside the methodWriteCData()
?– guijob
Yes
xml.WriteStartElement("nfeDadosMsg", URlSoapAction);
 xml.WriteCData(notaAssinada);
 xml.WriteEndElement();
 xml.WriteEndElement();
the notable variableAssinada is that it has the string containing xml– RonyElias
your noteAssinada has CDATA inside her?
– guijob
I was looking here and there’s a Cdata on the Qrcode tag and if I take the Cdata the Qrcode is invalid
– RonyElias
ah, so it really complicates... concatenate Cdatas is complicated but it works if you change the
]]>
for]]]]><![CDATA[>
, with the exception of the last]]>
– guijob
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.
– RonyElias