XML和JSON互相转换需要用到 Newtonsoft JSON.NET包,可以在VS菜单中,进入Tools|NuGet Package Manager|Manage NuGet Packages for Solution.搜索安装Newtonsoft JSON.NET包
//XML转换成JSON
- XmlDocument document = new XmlDocument(); //XmlDocument对象
- document.Load(@"D:\Books.xml"); //加载Xml文件
- string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(document);//转换成json文件
- textBlockResults.Text = json;
- System.IO.File.AppendAllText(@"D:\Books.json",json);//保存
//JSON转换成XML
- string json=System.IO.File.ReadAllText(@"D:\Books.json"); //json文件
- XmlDocument document=Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json); //转换成XML文件
- textBlockResults.Text = FormatText(document.DocumentElement as XmlNode,"","");

本文详细介绍使用NewtonsoftJSON.NET包实现XML与JSON格式互相转换的方法。通过具体代码实例,展示了如何加载XML文件并将其转换为JSON,以及如何将JSON文件转换回XML格式。
1093

被折叠的 条评论
为什么被折叠?



