Description. I must add a node to an XML document. The node that I have to add I build it perfectly, but I do not know how to insert it in the exact location I need inside the XML document.
Code.
With this code I build and insert the new element, but I am inserting it at the end of the document as a new child node of the root element FacturaElectronica
, and not where I want to:
private void AddIdentificacionPolitica(string path)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load(path);
try
{
result = "";
string uri = "http://www.w3.org/2000/09/xmldsig#";
string URI = "http://uri.etsi.org/01903/v1.3.2#";
XmlElement DigestMethod = xmlDoc.CreateElement("ds", "DigestMethod");
XmlElement DigestValue = xmlDoc.CreateElement("ds", "DigestValue");
XmlElement SignaturePolicyIdentifier = xmlDoc.CreateElement("xades", "SignaturePolicyIdentifier", URI);
XmlElement SignaturePolicyId = xmlDoc.CreateElement("xades", "SignaturePolicyId", URI);
SignaturePolicyIdentifier.AppendChild(SignaturePolicyId);
XmlElement SigPolicyId = xmlDoc.CreateElement("xades", "SigPolicyId", URI);
SignaturePolicyId.AppendChild(SigPolicyId);
XmlElement Identifier = xmlDoc.CreateElement("xades", "Identifier", URI);
Identifier.InnerText = "https://tribunet.hacienda.go.cr/docs/esquemas/2016/v4.1/Resolucion_Comprobantes_Electronicos_DGT-R-48-2016.pdf";
SigPolicyId.AppendChild(Identifier);
XmlElement SigPolicyHash = xmlDoc.CreateElement("xades", "SigPolicyHash", URI);
SignaturePolicyId.AppendChild(SigPolicyHash);
DigestMethod = xmlDoc.CreateElement("ds", "DigestMethod",uri);
DigestMethod.SetAttribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
DigestValue = xmlDoc.CreateElement("ds", "DigestValue",uri);
byte[] shaCertificate = { 0x06, 0xb3, 0x90, 0xb6, 0x45, 0xbb, 0x68, 0x3a, 0xde, 0x72, 0x8e, 0xb8, 0xf9, 0x79, 0x27, 0xd9, 0x18, 0x01, 0x67, 0xdb };
SHA256 sigPolicyHash = SHA256Managed.Create();
byte[] sigPolicyHashValue = sigPolicyHash.ComputeHash(shaCertificate);
DigestValue.InnerText = Convert.ToBase64String(sigPolicyHashValue);
SigPolicyHash.AppendChild(DigestMethod);
SigPolicyHash.AppendChild(DigestValue);
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(SignaturePolicyIdentifier, true));
xmlDoc.Save(path);
}
catch (Exception ex ){ result = ex.ToString(); }
}
Expected result. In the image I show the document as it should be, and I highlight in blue where I should insert the new node. With the arrows frame to the parent node.
Question. How can I insert it exactly there?
XML as text. This is the complete XML document as it should be:
<?xml version="1.0" encoding="UTF-8"?>
-<FacturaElectronica xmlns="https://tribunet.hacienda.go.cr/docs/esquemas/2017/v4.2/facturaElectronica" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Clave>50601011800020570010500100001010000000003100000003</Clave>
<NumeroConsecutivo>00100001010000000003</NumeroConsecutivo>
<FechaEmision>2018-01-01T19:35:42.539375-06:00</FechaEmision>
-<Emisor>
<Nombre>Jorge Eduardo Jaime Castro</Nombre>
-<Identificacion>
<Tipo>01</Tipo>
<Numero>205700105</Numero>
</Identificacion>
<NombreComercial>Jorge Eduardo Jaime Castro</NombreComercial>
-<Ubicacion>
<Provincia>2</Provincia>
<Canton>03</Canton>
<Distrito>01</Distrito>
<Barrio>08</Barrio>
<OtrasSenas>Grecia</OtrasSenas>
</Ubicacion>
-<Telefono>
<CodigoPais>506</CodigoPais>
<NumTelefono>83759107</NumTelefono>
</Telefono>
-<Fax>
<CodigoPais>506</CodigoPais>
<NumTelefono>83759107</NumTelefono>
</Fax>
<CorreoElectronico>[email protected]</CorreoElectronico>
</Emisor>
-<Receptor>
<Nombre>Dental Care</Nombre>
-<Identificacion>
<Tipo>02</Tipo>
<Numero>3001123208</Numero>
</Identificacion>
<NombreComercial/>
-<Ubicacion>
<Provincia>1</Provincia>
<Canton>01</Canton>
<Distrito>01</Distrito>
<Barrio>01</Barrio>
<OtrasSenas/>
</Ubicacion>
-<Telefono>
<CodigoPais>506</CodigoPais>
<NumTelefono>88888888</NumTelefono>
</Telefono>
-<Fax>
<CodigoPais>506</CodigoPais>
<NumTelefono>88888888</NumTelefono>
</Fax>
<CorreoElectronico>[email protected]</CorreoElectronico>
</Receptor>
<CondicionVenta>02</CondicionVenta>
<PlazoCredito>15</PlazoCredito>
<MedioPago>04</MedioPago>
-<DetalleServicio>
-<LineaDetalle>
<NumeroLinea>1</NumeroLinea>
-<Codigo>
<Tipo>04</Tipo>
<Codigo>3</Codigo>
</Codigo>
<Cantidad>1.000</Cantidad>
<UnidadMedida>Unid</UnidadMedida>
<UnidadMedidaComercial/>
<Detalle>Servicios profesionales</Detalle>
<PrecioUnitario>150000.00000</PrecioUnitario>
<MontoTotal>150000.00000</MontoTotal>
<NaturalezaDescuento/>
<SubTotal>150000.00000</SubTotal>
<MontoTotalLinea>150000.00000</MontoTotalLinea>
</LineaDetalle>
</DetalleServicio>
-<ResumenFactura>
<CodigoMoneda>USD</CodigoMoneda>
<TipoCambio>576.74000</TipoCambio>
<TotalServGravados>0.00000</TotalServGravados>
<TotalServExentos>150000.00000</TotalServExentos>
<TotalMercanciasGravadas>0.00000</TotalMercanciasGravadas>
<TotalMercanciasExentas>0.00000</TotalMercanciasExentas>
<TotalGravado>0.00000</TotalGravado>
<TotalExento>150000.00000</TotalExento>
<TotalVenta>150000.00000</TotalVenta>
<TotalDescuentos>0.00000</TotalDescuentos>
<TotalVentaNeta>150000.00000</TotalVentaNeta>
<TotalImpuesto>0.00000</TotalImpuesto>
<TotalComprobante>150000.00000</TotalComprobante>
</ResumenFactura>
-<Normativa>
<NumeroResolucion>DGT-R-48-2016</NumeroResolucion>
<FechaResolucion>20-02-2017 13:22:22</FechaResolucion>
</Normativa>
-<Otros>
<OtroTexto codigo="obs">BNCR $ 200-</OtroTexto>
</Otros>
-<ds:Signature Id="Signature-b9789560-18b0-4e2c-9a14-284b8ba64371" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
-<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
-<ds:Reference Id="Reference-dd5d9aec-0842-4c5c-a07e-7fdae964fd65" URI="">
-<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<ds:DigestValue>qYIYm7bmPDO7WdM1ycNmTMFeIRLbbY29NcLhjl/X/Rk=</ds:DigestValue>
</ds:Reference>
-<ds:Reference Id="ReferenceKeyInfo" URI="#KeyInfoId-Signature-b9789560-18b0-4e2c-9a14-284b8ba64371">
<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<ds:DigestValue>DWE0SSn6ORzQ26HXmBPwG2VztoFCMTyNY7d6iP574QQ=</ds:DigestValue>
</ds:Reference>
-<ds:Reference URI="#SignedProperties-Signature-b9789560-18b0-4e2c-9a14-284b8ba64371" Type="http://uri.etsi.org/01903#SignedProperties">
<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<ds:DigestValue>kb2iTXX9GbpIRoPzKwuO0JCyb+S/xb8L0vu1/DP6T6A=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue Id="SignatureValue-b9789560-18b0-4e2c-9a14-284b8ba64371">bRDp6XQUIlNlfi/1rid9OOw3RUv/njCIfh5dp4YBfcEmJWuP8x1XmHvpjP2M4ulqI9zEPVdZXU9s9xuVRazzcN60L5ORHH6ruaiu4kJ5w4A44YYyynm4vD8dm08Tm20h3chvRozP4yZpFsxJtMItUGORITB8Vf+CWtld46X1ABt77/H6B42uZodcZZS1D1jeXxlm5Trk7ECFyrj7ci/pDlQqOJGHUJjfZi+36Yy9qCOXQJYLfUcXostOsiE0hZZ2dVfuw0LY0Xnl0c7oeHE3YEhY58wSigTFYp76nM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uoueHAF6eClGhmslIJT/idHHOD4U27/JysQ0xAFctCoswGniR9Q7enx3sYvk0xGwpJy4uDePdyJ+eVfjq+/qlAxTwUTQNat0y0FIa6i1GetNbNXcAvI2TdiOEMAiBeRCKHvU4V5PS6u3RQB0SX0bEfzvxf+YrDiw9Yel+K/TarbsOr4luHOyIdpZay/Zci47T9W5FMQI1I8GE+FxHYnTKhmteyx41G0MjpwogUY2EM3ENHw==</ds:Modulus>
<ds:Exponent>AQAB</ds:Exponent>
</ds:RSAKeyValue>
</ds:KeyValue>
</ds:KeyInfo>
-<ds:Object Id="XadesObjectId-41071ab8-57e3-41dd-b91d-c11c9274a9f2">
-<xades:QualifyingProperties Id="QualifyingProperties-0cd36699-c9b0-4495-81dc-0d9ef335dc90" Target="#Signature-b9789560-18b0-4e2c-9a14-284b8ba64371" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#">
-<xades:SignedProperties Id="SignedProperties-Signature-b9789560-18b0-4e2c-9a14-284b8ba64371">
+<xades:SignedSignatureProperties>
+<xades:SignedDataObjectProperties>
</xades:SignedProperties>
</xades:QualifyingProperties>
</ds:Object>
</ds:Signature>
</FacturaElectronica>