Convert JSON to XML (edit)

using Newtonsoft.Json;
using System;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace Json2Xml
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ConvertJson2Xml();
            Console.Write("Successfully! Press any key to exit ");
            Console.ReadKey();
        }

        /// 
        /// Convert JSON to XML
        /// https://www.newtonsoft.com/json/help/html/ConvertJsonToXml.htm
        /// 1. Copy JSON from Web API
        /// 2. Format JSON from https://jsonformatter.org/ (replace ["] with [\"])
        /// 3. Convert JSON to XML
        /// 
        private static void ConvertJson2Xml()
        {
            string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string xmlRootFilePath = Path.Combine(desktopFolder, "root.xml");
            string jsonRootFilePath = Path.Combine(desktopFolder, "root.json");
            XmlDocument xmlDoc = new XmlDocument();
            XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
            var xmlDeclare = xmlDeclaration.OuterXml;
            string json = File.ReadAllText(jsonRootFilePath);
            XNode node = JsonConvert.DeserializeXNode(json, "root");
            string xmlContent = node.ToString();
            if (!xmlContent.Contains(xmlDeclare))
            {
                xmlContent = xmlContent.Insert(0, Environment.NewLine).Insert(0, xmlDeclare);
            }
            File.WriteAllText(xmlRootFilePath, xmlContent);
        }
    }
}

Sort XML

https://stackoverflow.com/questions/8749480/sort-xml-nodes-by-alpha-numeric-using-c-sharp

JSON Online Tools

Có chức năng view JSON dễ nhìn, trực quan

https://jsoneditoronline.org/

Có chức năng thu gọn JSON trên một dòng code

https://jsonformatter.org/