0
The issue is that the comprobante
tag's content has XML escape sequences <
and >
instead of the actual <
and >
characters. You'll need to decode these escape sequences back to their original form before saving the document. Here's an example in C#:
using System;
using System.IO;
using System.Xml;
class Program
{
static void Main()
{
// Your XML content
string xmlContent = @"<autorizaciones><autorizacion><estado>aaa</estado><numeroAutorizacion>2011202</numeroAutorizacion><fechaAutorizacion>2023-11-20T10:30:14-05:00</fechaAutorizacion><ambiente>prueba</ambiente><comprobante><?xml version=""1.0"" encoding=""UTF-8""?>
<factura id=""comprobante"" version=""1.0.0"">
<infoTributaria>
<ambiente>2</ambiente>
<tipoEmision>1</tipoEmision>
<razonSocial>EMPRESA</razonSocial>
<nombreComercial>EMPRESA</nombreComercial>
<ruc>0993222011001</ruc>
<claveAcceso>2011202</claveAcceso>
<codDoc>01</codDoc>
<estab>001</estab>
<ptoEmi>002</ptoEmi>
<secuencial>000107272</secuencial>
<dirMatriz>AV 25 DE Y 8 VO CLLJN</dirMatriz>
</infoTributaria></comprobante><mensajes /></autorizacion></autorizaciones>";
// Load the XML content into an XmlDocument
XmlDocument docXML = new XmlDocument();
docXML.LoadXml(xmlContent);
// Get the comprobante node and its inner text
XmlNode comprobanteNode = docXML.SelectSingleNode("//comprobante");
string comprobanteInnerXml = comprobanteNode.InnerXml;
// Decode the escaped XML content
string decodedContent = System.Net.WebUtility.HtmlDecode(comprobanteInnerXml);
// Update the comprobante node with CDATA section
comprobanteNode.InnerXml = $"<![CDATA[{decodedContent}]]>";
// Save the modified XML document to a file
string filePath = "modified_xml_file.xml";
docXML.Save(filePath);
Console.WriteLine($"XML saved to file: {filePath}");
}
}
This code uses System.Net.WebUtility.HtmlDecode
to decode the HTML-encoded content within the comprobante
tag. After that, it replaces the comprobante
tag's inner XML with a CDATA section and saves the modified XML document to a file.
Make sure to include necessary error handling and modifications based on your exact use case, such as specifying the proper file paths and handling any exceptions that may occur during file operations.
