Pessoal. Boa tarde.
Estou criando aplicacao c# para gerar o arquivo XML de NFServico eletronica.
a extrutura do XML é mais ou menos assim:
<CompNfse xmlns:xsi="...
<Nfse>
<InfNfse id="...
apos este node, deveria vir a assinatura, mas ela fica abaixo do <Nfse> quando o arquivo é gerado
fica assim:
<CompNfse xmlns:xsi="...
<Nfse>
<InfNfse id="...
</InfNfse>
</Nfse>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
...
... Alguem já teve este problema?
Eu sei que somente pelo pedaço de código é difícil de ajudar, mas pode ser que alguem
ja tenha passado pelo problema. Segue abaixo.
...
// Create a SignedXml object.
SignedXml signedXml = new SignedXml(doc);
// Add the key to the SignedXml document
signedXml.SigningKey = _X509Cert.PrivateKey;
// Create a reference to be signed
Reference reference = new Reference();
// pega o uri que deve ser assinada
XmlAttributeCollection _Uri = doc.GetElementsByTagName(RefUri).Item(0).Attributes;
foreach (XmlAttribute _atributo in _Uri)
{
if (_atributo.Name.ToLower() == "id")
{
reference.Uri = "#" + _atributo.InnerText;
}
}
// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
XmlDsigC14NTransform c14 = new XmlDsigC14NTransform();
reference.AddTransform(c14);
// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
// Create a new KeyInfo object
KeyInfo keyInfo = new KeyInfo();
// Load the certificate into a KeyInfoX509Data object
// and add it to the KeyInfo object.
keyInfo.AddClause(new KeyInfoX509Data(_X509Cert));
// Add the KeyInfo object to the SignedXml object.
signedXml.KeyInfo = keyInfo;
signedXml.ComputeSignature();
// Get the XML representation of the signature and save
// it to an XmlElement object.
XmlElement xmlDigitalSignature = signedXml.GetXml();
// Append the element to the XML document.
doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true));
XMLDoc = new XmlDocument();
XMLDoc.PreserveWhitespace = false;
XMLDoc = doc;
...