fork download
  1. /*
  2. Problema al agregar nodo a documento XML
  3. https://es.stackoverflow.com/q/131654/127
  4. */
  5. using System;
  6. using System.Xml;
  7. using System.Security.Cryptography;
  8.  
  9. public class Test
  10. {
  11. public static void Main()
  12. {
  13. //XML simplificado
  14. string xmlOriginal = @"<?xml version=""1.0"" encoding=""UTF-8""?>
  15. <FacturaElectronica xmlns=""https://t...content-available-to-author-only...o.cr/docs/esquemas/2017/v4.2/facturaElectronica"" xmlns:xsi=""http://w...content-available-to-author-only...3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://w...content-available-to-author-only...3.org/2001/XMLSchema"">
  16. <Clave>50601011800020570010500100001010000000003100000003</Clave>
  17. <Otros>...</Otros>
  18. <ds:Signature xmlns:ds=""http://w...content-available-to-author-only...3.org/2000/09/xmldsig#"" Id=""Signature-b9789560-18b0-4e2c-9a14-284b8ba64371"">
  19. <ds:SignedInfo>
  20. ...
  21. </ds:SignedInfo>
  22. <ds:SignatureValue Id=""SignatureValue-b9789560-18b0-4e2c-9a14-284b8ba64371"">...</ds:SignatureValue>
  23. <ds:KeyInfo Id=""KeyInfoId-Signature-b9789560-18b0-4e2c-9a14-284b8ba64371"">
  24. </ds:KeyInfo>
  25. <ds:Object Id=""XadesObjectId-41071ab8-57e3-41dd-b91d-c11c9274a9f2"">
  26. <xades:QualifyingProperties xmlns:xades=""http://u...content-available-to-author-only...i.org/01903/v1.3.2#"" Id=""QualifyingProperties-0cd36699-c9b0-4495-81dc-0d9ef335dc90"" Target=""#Signature-b9789560-18b0-4e2c-9a14-284b8ba64371"">
  27. <xades:SignedProperties Id=""SignedProperties-Signature-b9789560-18b0-4e2c-9a14-284b8ba64371"">
  28. <xades:SignedSignatureProperties>
  29. <xades:SigningTime>bla bla bla</xades:SigningTime>
  30. <xades:SigningCertificate>
  31. aca va todo el certificate
  32. </xades:SigningCertificate>
  33. </xades:SignedSignatureProperties>
  34. <xades:SignedDataObjectProperties>
  35. </xades:SignedDataObjectProperties>
  36. </xades:SignedProperties>
  37. </xades:QualifyingProperties>
  38. </ds:Object>
  39. </ds:Signature>
  40. </FacturaElectronica>";
  41.  
  42.  
  43. //TU CÓDIGO PARA GENERAR EL NODO NUEVO
  44. XmlDocument xmlDoc = new XmlDocument();
  45. xmlDoc.PreserveWhitespace = true;
  46. xmlDoc.LoadXml(xmlOriginal); // <-- acá lo estoy cargando manualmente desde el string
  47. //xmlDoc.Load(path);
  48. string result = "";
  49. try
  50. {
  51. string uri = "http://w...content-available-to-author-only...3.org/2000/09/xmldsig#";
  52. string URI = "http://u...content-available-to-author-only...i.org/01903/v1.3.2#";
  53. XmlElement DigestMethod = xmlDoc.CreateElement("ds", "DigestMethod");
  54. XmlElement DigestValue = xmlDoc.CreateElement("ds", "DigestValue");
  55. XmlElement SignaturePolicyIdentifier = xmlDoc.CreateElement("xades", "SignaturePolicyIdentifier", URI);
  56. XmlElement SignaturePolicyId = xmlDoc.CreateElement("xades", "SignaturePolicyId", URI);
  57. SignaturePolicyIdentifier.AppendChild(SignaturePolicyId);
  58. XmlElement SigPolicyId = xmlDoc.CreateElement("xades", "SigPolicyId", URI);
  59. SignaturePolicyId.AppendChild(SigPolicyId);
  60. XmlElement Identifier = xmlDoc.CreateElement("xades", "Identifier", URI);
  61. Identifier.InnerText = "https://t...content-available-to-author-only...o.cr/docs/esquemas/2016/v4.1/Resolucion_Comprobantes_Electronicos_DGT-R-48-2016.pdf";
  62. SigPolicyId.AppendChild(Identifier);
  63. XmlElement SigPolicyHash = xmlDoc.CreateElement("xades", "SigPolicyHash", URI);
  64. SignaturePolicyId.AppendChild(SigPolicyHash);
  65. DigestMethod = xmlDoc.CreateElement("ds", "DigestMethod",uri);
  66. DigestMethod.SetAttribute("Algorithm", "http://w...content-available-to-author-only...3.org/2001/04/xmlenc#sha256");
  67. DigestValue = xmlDoc.CreateElement("ds", "DigestValue",uri);
  68. byte[] shaCertificate = { 0x06, 0xb3, 0x90, 0xb6, 0x45, 0xbb, 0x68, 0x3a, 0xde, 0x72, 0x8e, 0xb8, 0xf9, 0x79, 0x27, 0xd9, 0x18, 0x01, 0x67, 0xdb };
  69. SHA256 sigPolicyHash = SHA256Managed.Create();
  70. byte[] sigPolicyHashValue = sigPolicyHash.ComputeHash(shaCertificate);
  71. DigestValue.InnerText = Convert.ToBase64String(sigPolicyHashValue);
  72. SigPolicyHash.AppendChild(DigestMethod);
  73. SigPolicyHash.AppendChild(DigestValue);
  74.  
  75.  
  76.  
  77. //ACÁ EL CÓDIGO PARA INSERTARLO
  78.  
  79. //Crear un XmlNamespaceManager para poder buscar en el namespace "xades:"
  80. XmlNamespaceManager nsManager = new XmlNamespaceManager(xmlDoc.NameTable);
  81. nsManager.AddNamespace("xades", URI);
  82.  
  83. //Buscar con XPath y seleccionar el primer nodo "xades:SignedSignatureProperties"
  84. XmlNode SignedSignatureProperties = xmlDoc.SelectSingleNode("//xades:SignedSignatureProperties", nsManager);
  85.  
  86. //Si se encontró el nodo padre, insertarle el nuevo
  87. if (SignedSignatureProperties != null) {
  88. SignedSignatureProperties.AppendChild(SignaturePolicyIdentifier);
  89. }
  90.  
  91.  
  92. //IMPRIMIR EN CONSOLA
  93. //xmlDoc.Save(path);
  94. XmlTextWriter writer = new XmlTextWriter(Console.Out);
  95. writer.Formatting = Formatting.Indented;
  96. xmlDoc.WriteTo( writer );
  97. writer.Flush();
  98. Console.WriteLine();
  99. }
  100. catch (Exception ex ){
  101. result = ex.ToString();
  102. Console.WriteLine(result);
  103. }
  104. }
  105. }
Success #stdin #stdout 0.05s 134848KB
stdin
Standard input is empty
stdout
<?xml version="1.0" encoding="UTF-8"?>

<FacturaElectronica xmlns="https://t...content-available-to-author-only...o.cr/docs/esquemas/2017/v4.2/facturaElectronica" xmlns:xsi="http://w...content-available-to-author-only...3.org/2001/XMLSchema-instance" xmlns:xsd="http://w...content-available-to-author-only...3.org/2001/XMLSchema">
  <Clave>50601011800020570010500100001010000000003100000003</Clave>
  <Otros>...</Otros>
  <ds:Signature xmlns:ds="http://w...content-available-to-author-only...3.org/2000/09/xmldsig#" Id="Signature-b9789560-18b0-4e2c-9a14-284b8ba64371">
    <ds:SignedInfo>
      ...
    </ds:SignedInfo>
    <ds:SignatureValue Id="SignatureValue-b9789560-18b0-4e2c-9a14-284b8ba64371">...</ds:SignatureValue>
    <ds:KeyInfo Id="KeyInfoId-Signature-b9789560-18b0-4e2c-9a14-284b8ba64371">
    </ds:KeyInfo>
    <ds:Object Id="XadesObjectId-41071ab8-57e3-41dd-b91d-c11c9274a9f2">
      <xades:QualifyingProperties xmlns:xades="http://u...content-available-to-author-only...i.org/01903/v1.3.2#" Id="QualifyingProperties-0cd36699-c9b0-4495-81dc-0d9ef335dc90" Target="#Signature-b9789560-18b0-4e2c-9a14-284b8ba64371">
        <xades:SignedProperties Id="SignedProperties-Signature-b9789560-18b0-4e2c-9a14-284b8ba64371">
          <xades:SignedSignatureProperties>
            <xades:SigningTime>bla bla bla</xades:SigningTime>
            <xades:SigningCertificate>
              aca va todo el certificate
            </xades:SigningCertificate>
          <xades:SignaturePolicyIdentifier><xades:SignaturePolicyId><xades:SigPolicyId><xades:Identifier>https://t...content-available-to-author-only...o.cr/docs/esquemas/2016/v4.1/Resolucion_Comprobantes_Electronicos_DGT-R-48-2016.pdf</xades:Identifier></xades:SigPolicyId><xades:SigPolicyHash><ds:DigestMethod Algorithm="http://w...content-available-to-author-only...3.org/2001/04/xmlenc#sha256" /><ds:DigestValue>7XUCtw/f4o/yTilOqK8RXLFzYGeHMw9luOdMzLbCU/w=</ds:DigestValue></xades:SigPolicyHash></xades:SignaturePolicyId></xades:SignaturePolicyIdentifier></xades:SignedSignatureProperties>
          <xades:SignedDataObjectProperties>
</xades:SignedDataObjectProperties>
        </xades:SignedProperties>
      </xades:QualifyingProperties>
    </ds:Object>
  </ds:Signature>
</FacturaElectronica>