1
Good afternoon.
I am trying to sign the XML, but I have the error below:
Error generating System.Security.Cryptography.Cryptographicexception file: Poorly formed Reference Element.
Searching, I saw that the problem is in the referent line.Uri = "#" + id;
The id variable is with the value "ID1122632750001052017122616320300001"
Someone went through something like this?
Follows the code:
public static void GeraXMLAssinado(String caminho){
XmlDocument doc = new XmlDocument();
doc.Load(caminho);
XmlNodeList ListInfNFe = doc.GetElementsByTagName("evento");
foreach (XmlElement infNFe in ListInfNFe)
{
string id = infNFe.Attributes.GetNamedItem("Id").Value;
SignedXml xml = new SignedXml(infNFe);
xml.SigningKey = AssinaXMl().PrivateKey;
// Transformações p/ DigestValue da Nota
Reference reference = new Reference();
reference.Uri = "#" + id;
reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
reference.AddTransform(new XmlDsigC14NTransform());
xml.AddReference(reference);
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new KeyInfoX509Data(AssinaXMl()));
xml.KeyInfo = keyInfo;
xml.ComputeSignature();
XmlElement xmlSignature = doc.CreateElement("Signature", "http://www.w3.org/2000/09/xmldsig#");
XmlElement xmlSignedInfo = xml.SignedInfo.GetXml();
XmlElement xmlKeyInfo = xml.KeyInfo.GetXml();
XmlElement xmlSignatureValue = doc.CreateElement("SignatureValue", xmlSignature.NamespaceURI);
string signBase64 = Convert.ToBase64String(xml.Signature.SignatureValue);
XmlText text = doc.CreateTextNode(signBase64);
xmlSignatureValue.AppendChild(text);
xmlSignature.AppendChild(xmlSignatureValue);
xmlSignature.AppendChild(doc.ImportNode(xmlSignedInfo, true));
xmlSignature.AppendChild(doc.ImportNode(xmlKeyInfo, true));
doc.AppendChild(xmlSignature);
doc.Save(caminho);
}
}