Answers for "how to convert xml to json in c#"

C#
0

c# xml to json

string xml = @"<?xml version='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 json = JsonConvert.SerializeXmlNode(doc);

Console.WriteLine(json);
// {
//   "?xml": {
//     "@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"
//       }
//     ]
//   }
// }
Posted by: Guest on March-15-2022
0

Convert xml to json

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Xml;
using System.Xml.Serialization;

namespace create_plugin
{
    class Program
    {
        static void Main(string[] args)
        {
             string xml = null;
            using (WebClient wc = new WebClient())
            {
               xml = wc.DownloadString("https://www.cbar.az/currencies/15.03.2022.xml");
            }
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            string json = JsonConvert.SerializeXmlNode(doc);
            Console.WriteLine(json);
        }
    }
}
Posted by: Guest on March-15-2022

C# Answers by Framework

Browse Popular Code Answers by Language