I need help converting huge Xml to Json format.
I've been researching about this topic. I found this:
How to convert JSON to XML or XML to JSON?
Reading large XML documents in .net
Reading and manpulating large xml of 1 GB
Well, The easy way is something like that:
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);
but I can't use it because my file is huge (2GB) So I get OutOfMemoryException
.
So, I need another way for read the large file. I've been using this way:
using (XmlReader xr = XmlReader.Create(inputPath))
{
while (xr.Read())
{
switch(xr.NodeType)
{
case XmlNodeType.Element:
//Do things
break;
case XmlNodeType.Text:
//Do things
break;
case XmlNodeType.EndElement:
//Do things
break;
}
}
}
I read the xml file and convert from xml to json concatenating strings tag by tag. But it's convoluted and extremely inefficient and It doesn't work correctly.
When i was researching, i found LINQ to XML. But I don't know how to use it. I think is good for manipulating and filter the huge xml but i need to read the whole file.
My Xml file looks like:
<?xml version="1.0" encoding="utf-8"?>
<root>
<key>
<item> value </value>
<item> value2 </value>
<item> value3 </value>
</key>
<id>1</id>
<name>Foo</name>
<hugeArray> //This array has around 12 millions of entries. Here is my problem.
<item>
<direction> </direction>
<companyId> </companyId>
<nameId> </nameId>
</item>
<item>
<direction> </direction>
<companyId> </companyId>
<nameId> </nameId>
</item>
....
</hugeArray>
</root>
I found my problems with the array. I don't know how to cut and read it.
How should i read the whole file? How should i write the json?
I was concatenating characteres but i could use JsonWriter
class.
UPDATE:
The algorithm should be able to convert from any xml to json.