@manhnguyenv

Welcome to my blog!

Convert JSON to XML

October 6, 2018 11:13

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/

Web API AND XML

March 15, 2018 21:27

Code

Ví dụ

http://www.lateral8.com/articles/2010/3/5/openxml-sdk-20-export-a-datatable-to-excel.aspx

<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>AccountProblem</Code>
<Message>Your Google account is not currently enabled for this operation. Please check https://console.developers.google.com/billing to see if you have a past due balance or if the credit card (or other payment mechanism) on your account is expired. You can find additional information at https://developers.google.com/storage/docs/signup</Message>
<Details>The billing account for the requested project is disabled in state 'closed'</Details>
</Error>

Categories

Recent posts