ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

XML to Json

XML to Json

Source

//// Install nuget package for Newtonsoft json
//// https://www.nuget.org/packages/Newtonsoft.Json/
//// Install-Package Newtonsoft.Json

 public static string ToJSON(this string xml)
{
    // To convert an XML node contained in string xml into a JSON string
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    return JsonConvert.SerializeXmlNode(doc);
}

Example

String xmlString = "C:my.xml";
var jsonResult = xmlString.ToJSON();

Author: Keyur Panchal

Submitted on: 11 aug 2015

Language: C#

Type: XML, JSON, C#

Views: 6075