Posts

Showing posts from February, 2017

Convert content of XML file to JSON string in C#.Net

Convert content of XML file to JSON string in C#.Net
The below function will convert the content of XML file to JSON string:


privatestringconvertXMLToJSON() { string xmlLogPath = "<<Path to the xml file>>"; // Load the XML filevar xDocument = System.Xml.Linq.XDocument.Load(xmlLogPath); // Read the XML file content and write it to stringstring xml = xDocument.ToString(); // Load the XML string XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); // Convert the XML string to JSON stringstring jsonText = JsonConvert.SerializeXmlNode(doc); //Release the memory doc = null; xmlLogPath = null; xml = null; xDocument = null; //Return the JSON stringreturn jsonText; } IMPORTANT NOTE:

Releasing object memory is very important. Even if your object went out of scope, it is not removed from the…