1

I have an XML file. I want to convert it to JSON with C#. However, the XML file is over 20 GB.

I have tried to read XML with XmlReader, then append every node to a JSON file. I wrote the following code:

var path = @"c:\result.json";
TextWriter tw = new StreamWriter(path, true, Encoding.UTF8);
tw.Write("{\"A\":");

using (XmlTextReader xmlTextReader = new XmlTextReader("c:\\muslum.xml"))
{
    while (xmlTextReader.Read())
    {
        if (xmlTextReader.Name == "A")
        {
            var xmlDoc = new XmlDocument();
            var v = xmlTextReader.ReadInnerXml();

            string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xmlDoc, Newtonsoft.Json.Formatting.None, true);

            tw.Write(json);
        }
    }
}

tw.Write("}");
tw.Close();

This code not working. I am getting error while converting json. Is there any best way to perform the conversion?

David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
Müslüm ÖZTÜRK
  • 961
  • 1
  • 11
  • 30

1 Answers1

2

I would do it the following way

  • generate classes out of xsd schema using xsd.exe

  • open file and read top level ( i e your document level ) tags one by one (with XmlTextReader or XmlReader)

  • serialize each tag into object using generated classes

  • deserialize resulting object to json and save to whatever

  • consider saving in batches of 1000-2000 tags

  • you are right about serialize/deserialize being slow. still doing work in several threads, preferably using TPL will give you good speed. Also consider using json.net serializer, it is really a lot faster than standard ones ( it is standard for web.api though)

I can put some code snippets in the morning if you need them. We are processing big ( 1-10gigs) files this manner in order to save data to sql server database.

vittore
  • 17,449
  • 6
  • 44
  • 82
  • I thought this. But serialize and deserialize processes take long time. So Importing xml to db get very long time. I asked the question find the fastest way. I will be happy if you share code snippets. Thanks – Müslüm ÖZTÜRK Jan 28 '14 at 06:51