1

I am facing the problem with converting XML file to JSON with following code:

Console.WriteLine("Start");

string inputPath = "input.xml";

// Load XML document
XmlDocument doc = new XmlDocument();
doc.Load(inputPath);

string outputPath = "output.json";
File.WriteAllText(outputPath, JsonConvert.SerializeXmlNode(doc));

Console.Write("End");

Console.ReadKey();

The file fills memory and that's problem. I didn't find solution without fill RAM.

How can I accomplish that? Thank you very much

Adam Gajdečka
  • 305
  • 1
  • 4
  • 13
  • 2
    How big is the XML file? – mason Jun 14 '21 at 15:40
  • I need convert unknown json files on server, so performance is very important. I didn't find the solution on link what you sent. – Adam Gajdečka Jun 14 '21 at 15:53
  • I am looking for solution with XmlReader or something like that. – Adam Gajdečka Jun 14 '21 at 15:57
  • What do you call a "*huge*" file? What size? How can that fill the memory? What memory? You should also take into consideration that XML and JSON are not for example CSV and the format can vary from compact to stretched like HTML and CS for example, so you can't read line by line... in a pinch you could read the file byte by byte and determine tags, if no component already exists, maybe, I don't know, I haven't searched the web or https://nugetmusthaves.com/ and https://github.com/ –  Jun 14 '21 at 15:59
  • It can be up to 1 GB and I need convert many files at same time (it depends on users of my app). My solution above fills RAM and that's not a way to do that. – Adam Gajdečka Jun 14 '21 at 16:04
  • 1
    I need to convert many files for my users. Could you imagine convert 1 GB file for 1000 users at the same time? It require 1000 GB of RAM. It's insane! It's not acceptable – Adam Gajdečka Jun 14 '21 at 16:17
  • 1
    Ok, I see, you are on a webserver or a mainframe that support 1000 requests at a time? I don't know XmlReader nor if it can match, thus see for example https://zetcode.com/csharp/xmlreader/ and https://csharp.net-tutorials.com/xml/reading-xml-with-the-xmlreader-class/ –  Jun 14 '21 at 16:19
  • have a look at https://stackoverflow.com/questions/814001/how-to-convert-json-to-xml-or-xml-to-json/50118541#50118541 – Cinchoo Jun 16 '21 at 23:26

0 Answers0