Someone can help me, I had in a XMLDocument variable the following:
I want to save to a file in the following format:
I use docXML.Save(file) but the first format is preserve. How can i do to resolv the problem?
Regards

Prasad RaveendranRank #320 · 7.8K reputationPosted Nov 21, 2023, 6:45 PM EST
The issue is that the
comprobantetag'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 = @"aaa20112022023-11-20T10:30:14-05:00prueba<?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>"; // 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 = $""; // 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.HtmlDecodeto decode the HTML-encoded content within thecomprobantetag. After that, it replaces thecomprobantetag'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.