C#中使用JSON.NET实现JSON、XML相互转换_C#教程-查字典教程网
C#中使用JSON.NET实现JSON、XML相互转换
C#中使用JSON.NET实现JSON、XML相互转换
发布时间:2016-12-28 来源:查字典编辑
摘要:官方JSON.NET地址http://james.newtonking.com/pages/json-net.aspxXMLTOJSONst...

官方 JSON.NET 地址

http://james.newtonking.com/pages/json-net.aspx

XML TO JSON

string xml = @"<"1.0"" standalone=""no""?> <root> <person id=""1""> <name>Alan</name> <url>http://www.google.com</url> </person> <person id=""2""> <name>Louis</name> <url>http://www.yahoo.com</url> </person> </root>"; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); string jsonText = JsonConvert.SerializeXmlNode(doc); //{ // ""@version": "1.0", // "@standalone": "no" // }, // "root": { // "person": [ // { // "@id": "1", // "name": "Alan", // "url": "http://www.google.com" // }, // { // "@id": "2", // "name": "Louis", // "url": "http://www.yahoo.com" // } // ] // } //}

JSON TO XML

string json = @"{ """@version"": ""1.0"", ""@standalone"": ""no"" }, ""root"": { ""person"": [ { ""@id"": ""1"", ""name"": ""Alan"", ""url"": ""http://www.google.com"" }, { ""@id"": ""2"", ""name"": ""Louis"", ""url"": ""http://www.yahoo.com"" } ] } }"; XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json); // <"1.0" standalone="no"?> // <root> // <person id="1"> // <name>Alan</name> // <url>http://www.google.com</url> // </person> // <person id="2"> // <name>Louis</name> // <url>http://www.yahoo.com</url> // </person> // </root>

DEMO:JSON TO XML

string json_str = "{"a":"a","b":"b"}"; //json 的字符串需要按照这个格式 书写,否则会报错 string json = @"{ """@version"": ""1.0"", ""@standalone"": ""no"" }, ""root"":" + json_str + "}"; if (!string.IsNullOrEmpty(json)) { XmlDocument doc = JsonConvert.DeserializeXmlNode(json); }

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新C#教程学习
    热门C#教程学习
    编程开发子分类