usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Xml;usingNewtonsoft;//This method needs to be downloaded separatelynamespacethumbslotteryticket.mymethond{ Public classXml_json {/// <summary> ///returns a JSON-formatted string of information under the specified node/// </summary> /// <param name= "str" >XML string</param> /// <param name= "NodeName" >node name, starting at the root node</param> /// <returns></returns> Public stringXml2json (stringStrstringnodename) { stringresult =NULL; XmlDocument xmldoc=NewXmlDocument (); XmlDoc. LOADXML (str); XmlNode node=xmldoc. selectSingleNode (nodename); Result=Newtonsoft.Json.JsonConvert.SerializeXmlNode (node); returnresult; } Public stringJson2xml (stringstr) { stringresult =NULL; XmlDocument XML=Newtonsoft.Json.JsonConvert.DeserializeXmlNode (str); Result=XML. OuterXml; returnresult; } }
XML and JSON in C # convert each other