Sorting XML While Converting it to JsonString



Here is sample of XML I have its unsorted



<hierarchy>
<date>2015/02/27 16:37:10</date>
<folder name="Root" id="Root">
<file id="Erstg_20.xlsx" />
<file id="AAERG_20.xlsx" />
<folder name="Xmdg" id="Xmdg">
<file id="DatePicker_20.xlsx" />
<file id="Abcd_20.xlsx" />
</folder>
<folder name="Axcd" id="Axcd">
<file id="Zfcd_20.xlsx" />
<file id="Abcd_20.xlsx" />
</folder>
</folder>
</hierarchy>


I want to sort it in a way that all folders in Root node should be sorted in ascending order and files within every node should be sorted in ascending order according to their Id's


Here is how i am doing it right now



XmlDocument doc = new XmlDocument();
doc.Load(xmlFilePath);
try {

string json = JsonConvert.SerializeXmlNode(doc);
string rJson = json.Replace("@id", "id");
json = rJson.Replace("@name", "name");
System.IO.File.WriteAllText(HierarchyJSFilePath, "var jsonStr= " + json + ";");

}

No comments:

Post a Comment