Last active
April 20, 2021 12:22
-
-
Save jj09/c401d7066a2df1437987d4803fffcdb6 to your computer and use it in GitHub Desktop.
Convert XML to JSON in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System.IO; | |
using System.Xml; | |
namespace ConvertXml2Json | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const string DirectoryPath = @"../../../shanselman-blog/"; | |
string[] files = Directory.GetFiles(DirectoryPath); | |
foreach (string fileName in files) | |
{ | |
ProcessFile(fileName); | |
} | |
} | |
private static void ProcessFile(string fileName) | |
{ | |
if (fileName.Contains("feedback")) | |
{ | |
return; | |
} | |
string xml = File.ReadAllText(fileName); | |
XmlDocument doc = new XmlDocument(); | |
doc.LoadXml(xml); | |
string json = JsonConvert.SerializeXmlNode(doc); | |
File.WriteAllText(fileName.Replace(".xml", ".json"), json); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent example, recently I published similar article on Converting JSON to XML and Vice-versa in C#.
Using "Newtonsoft.Json" and without using it also, might be helpful for developers, so here is the link
Converting JSON to XML OR XML to JSON using C#
Thanks