Estoy tratando de agregar elementos y atributos desde código... El primer elemento Reference lo agrega bien pero al tratar de agregar el segundo me sale un error que dice :
Elemento Reference mal formado.
Este es mi codigo :
public void FirmarXadesEPES(XmlDocument xmlDoc, X509Certificate2 cert)
{
if (xmlDoc == null)
throw new ArgumentException("xmlDoc");
if (cert == null)
throw new ArgumentException("Cert");
String keyInfoID = "keyinfoID";
String signedPropertiestypeID = "SignedPropertiestypeID";
String signatureID = "FacturaeSignatureID";
XadesSignedXml signedXml = new XadesSignedXml(xmlDoc);
signedXml.SigningKey = (RSACryptoServiceProvider)cert.PrivateKey;
Reference reference = new Reference();
reference.Uri = "";
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
signedXml.AddReference(reference);
// AQUI EL ERROR
Reference keyInfoReference = new Reference();
keyInfoReference.Uri = "#" + keyInfoID;
env = new XmlDsigEnvelopedSignatureTransform();
keyInfoReference.AddTransform(env);
signedXml.AddReference(keyInfoReference);
Reference signedProperiestypeReference = new Reference();
signedProperiestypeReference.Uri = "#" + signedPropertiestypeID;
signedProperiestypeReference.Type = "http://uri.etsi.org/01903#SignedProperties";
env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
signedXml.AddReference(signedProperiestypeReference);
KeyInfo keyInfo = new KeyInfo();
keyInfo.Id = keyInfoID;
keyInfo.AddClause(new KeyInfoX509Data(cert));
signedXml.KeyInfo = keyInfo;
QualifyingPropertiesType qualifyingProperties = new QualifyingPropertiesType();
qualifyingProperties.Target = "#" + signatureID;
qualifyingProperties.SignedProperties = new SignedPropertiesType();
qualifyingProperties.SignedProperties.Id = signedPropertiestypeID;
qualifyingProperties.SignedProperties.SignedSignatureProperties = new SignedSignaturePropertiesType();
qualifyingProperties.SignedProperties.SignedSignatureProperties.SigningTime = DateTime.Today;
qualifyingProperties.SignedProperties.SignedSignatureProperties.SignaturePolicyIdentifier = new SignaturePolicyIdentifierType();
SignaturePolicyIdType signaturePolicyIdType = new SignaturePolicyIdType();
signaturePolicyIdType.SigPolicyId =new ObjectIdentifierType();
signaturePolicyIdType.SigPolicyId.Identifier=new IdentifierType();
signaturePolicyIdType.SigPolicyId.Identifier.Value = "http://www.facturae.es/politica_de_firma_formato_facturae/politica_de_firma_formato_facturae_v3_1.pdf";
signaturePolicyIdType.SigPolicyId.Description = "facturae31";
signaturePolicyIdType.SigPolicyHash = new DigestAlgAndValueType();
signaturePolicyIdType.SigPolicyHash.DigestMethod = new DigestMethodType2();
signaturePolicyIdType.SigPolicyHash.DigestMethod.Algorithm = "http://www.w3.org/2000/09/xmldsig#sha1";
signaturePolicyIdType.SigPolicyHash.DigestValue = Convert.FromBase64String("Ohixl6upD6av8N7pEvDABhEL6hM=");
qualifyingProperties.SignedProperties.SignedSignatureProperties.SignaturePolicyIdentifier.Item = signaturePolicyIdType;
//signedXml.AddQualifyingPropertiesObject(qualifyingProperties);
signedXml.ComputeSignature();
XmlElement xmlDigitalSignature = signedXml.GetXml();
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
}