C# serialize xml to json via NewtonSoft.Json SerializeXmlNode
Install-Package Newtonsoft.json
static void ConvertXmlToJson(string xmlFile,string jsonFile) { try { if(File.Exists(xmlFile)) { using(FileStream fs=new FileStream(xmlFile, FileMode.Open, FileAccess.Read)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fs); Console.WriteLine("Load successfully!"); string jsonStr=JsonConvert.SerializeXmlNode(xmlDoc,Newtonsoft.Json.Formatting.Indented); Console.WriteLine("Serialize json successfully!"); using(StreamWriter streamWriter=new StreamWriter(jsonFile,false,System.Text.Encoding.UTF8)) { streamWriter.WriteLine(jsonStr); Console.WriteLine($"ConvertXmlToJson done!"); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
using System.Diagnostics; using System.Xml; using System.Xml.Serialization; using Newtonsoft.Json; namespace ConsoleApp9 { internal class Program { static string xmlFile = "testserializetoxml.xml"; static string jsonFile = "testjson.json"; static void Main(string[] args) { Task.Run(() => { MonitorMemoryCost(); }); SerializeListTToXmlDemo(); ConvertXmlToJson(xmlFile, jsonFile); } static void ConvertXmlToJson(string xmlFile,string jsonFile) { try { if(File.Exists(xmlFile)) { using(FileStream fs=new FileStream(xmlFile, FileMode.Open, FileAccess.Read)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fs); Console.WriteLine("Load successfully!"); string jsonStr=JsonConvert.SerializeXmlNode(xmlDoc,Newtonsoft.Json.Formatting.Indented); Console.WriteLine("Serialize json successfully!"); using(StreamWriter streamWriter=new StreamWriter(jsonFile,false,System.Text.Encoding.UTF8)) { streamWriter.WriteLine(jsonStr); Console.WriteLine($"ConvertXmlToJson done!"); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } static void SerializeListTToXmlDemo() { List<Book> booksList = new List<Book>(); InitData(ref booksList); SerializeListTToXml<Book>(booksList, xmlFile); Console.WriteLine($"SerializeListTToXmlDemo() done!"); } static void SerializeListTToXml<T>(List<T> dataList,string xmlFileName) { try { XmlSerializer serializer = new XmlSerializer(typeof(List<T>)); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.Encoding = System.Text.Encoding.UTF8; var xmlWriter = XmlWriter.Create(xmlFileName, settings); serializer.Serialize(xmlWriter, dataList); xmlWriter.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } private static void InitData(ref List<Book> booksList) { for (int i = 0; i < 1000000; i++) { booksList.Add(new Book() { Id = i + 1, ISBN = $"ISBN_{i + 1}_{Guid.NewGuid().ToString()}", Name = $"Name_{i + 1}", Title = $"Title_{i + 1}", Topic = $"Topic_{i + 1}" }); } } static void MonitorMemoryCost() { while (true) { var proc=Process.GetCurrentProcess(); string costMemory = (proc.PrivateMemorySize64 / 1024 /1024).ToString("N0"); Console.WriteLine($"{DateTime.UtcNow},Memory {costMemory} M"); Thread.Sleep(1000); } } } public class Book { public int Id { get; set; } public string ISBN { get; set; } public string Name { get; set; } public string Title { get; set; } public string Topic { get; set; } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2023-12-14 cpp mysql lmysqlcppconn call procedure
2021-12-14 C++ array container reverse_iterator rbegin rend