[Solved] How to sort easily a XML file in C#? [closed]


This might work for you

var xml = new XmlDocument();
xml.LoadXml("<colors>" + 
            "<green>150</green>" +
            "<red>18</red>" + 
            "<blue>920</blue>" +
            "<orange>80</orange>" +
            "<purple>77</purple>" +
            "</colors>");

var lst = new Dictionary<string,int>();
foreach (XmlNode n in xml["colors"].ChildNodes)
    lst.Add(n.Name, int.Parse(n.InnerText));

var sb = new StringBuilder();
foreach (KeyValuePair<string, int> n in lst.OrderBy(kvp => kvp.Value))
    sb.AppendFormat("#define {0}   {1}    // <{0}>\n", n.Key, n.Value);

string result = sb.ToString();

The result (you might use C# string padding):

#define red   18    // <red>
#define purple   77    // <purple>
#define orange   80    // <orange>
#define green   150    // <green>
#define blue   920    // <blue>

8

solved How to sort easily a XML file in C#? [closed]