Sort XML nodes alphabetically on attribute name

前端 未结 3 1434
栀梦
栀梦 2021-01-07 13:17

I have an XML document for which I want to sort specific nodes alphabetically.

XML document


    

        
3条回答
  •  迷失自我
    2021-01-07 14:02

    I tried the following code and it is working (C#) you can get values as below

            var doc = new XmlDocument();
            doc.Load("c:\\users\\ozgur\\sample.xml");
            var nav = doc.CreateNavigator();
            var node = nav.SelectSingleNode("response").SelectSingleNode("lst[@name=\"facet_counts\"]").SelectSingleNode("lst[@name=\"facet_fields\"]").SelectSingleNode("lst[@name=\"professions_raw_nl\"]").Select("int");
    
            var sorted = new SortedDictionary();
            while (node.MoveNext())
            {
                var name = node.Current.SelectSingleNode("@name").Value;
                var value = node.Current.Value;
                sorted.Add(name, value);
            }
    
            foreach (var item in sorted)
            {
                item.Key.ToString();
                item.Value.ToString();
            }
    

提交回复
热议问题