JSON.NET: CONVERT JSON STRING TO XML OR XML TO JSON STRING

Jayden Bell

Total Post:110

Points:774
Posted by  Jayden Bell
JSON 
 2080  View(s)
Ratings:
Rate this:
Hi Everyone,

I started to use Json.NET to convert a string in JSON format to object or vice-versa. I am not sure in the JSON.NET framework, is it possible to convert a string in 

JSON to XML format and vice-versa?

Thanks in advance. 
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: Json.NET: convert JSON string to XML or XML to JSON string

    Hi Jayden!


    Yes. Using the JsonConvert class which contains helper methods for this precise purpose:

    // To convert an XML node contained in string xml into a JSON string   
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    string jsonText = JsonConvert.SerializeXmlNode(doc);

    // To convert JSON text contained in string json into an XML node
    XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!