1
Answer

XM Conversion CDATA

Luis Andaluz

Luis Andaluz

1y
687
1

Someone can help me, I had in a XMLDocument variable the following:

<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>

I want to save to a file in the following format:

<autorizaciones><autorizacion><estado>aaa</estado><numeroAutorizacion>2011202</numeroAutorizacion><fechaAutorizacion>2023-11-20T10:30:14-05:00</fechaAutorizacion><ambiente>prueba</ambiente><comprobante><![CDATA[<?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></factura>]]></comprobante><mensajes /></autorizacion></autorizaciones>

I use docXML.Save(file) but the first format is preserve.  How can i do to resolv the problem?

Regards

Answers (1)
0
Prasad Raveendran

Prasad Raveendran

237 8.3k 1.9m 1y

The issue is that the comprobante tag's content has XML escape sequences &lt; and &gt; 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>&lt;?xml version=""1.0"" encoding=""UTF-8""?&gt;
&lt;factura id=""comprobante"" version=""1.0.0""&gt;
  &lt;infoTributaria&gt;
    &lt;ambiente&gt;2&lt;/ambiente&gt;
    &lt;tipoEmision&gt;1&lt;/tipoEmision&gt;
    &lt;razonSocial&gt;EMPRESA&lt;/razonSocial&gt;
    &lt;nombreComercial&gt;EMPRESA&lt;/nombreComercial&gt;
    &lt;ruc&gt;0993222011001&lt;/ruc&gt;
    &lt;claveAcceso&gt;2011202&lt;/claveAcceso&gt;
    &lt;codDoc&gt;01&lt;/codDoc&gt;
    &lt;estab&gt;001&lt;/estab&gt;
    &lt;ptoEmi&gt;002&lt;/ptoEmi&gt;
    &lt;secuencial&gt;000107272&lt;/secuencial&gt;
    &lt;dirMatriz&gt;AV 25 DE Y 8 VO CLLJN&lt;/dirMatriz&gt;
  &lt;/infoTributaria&gt;</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.