测试资料:

  1. <Config>
  2. <Item a='' b='' c='' m=''/>
  3. <Item a='' b='' c='' m=''/>
  4. <Item a='' b='' c='' m=''/>
  5. <Item a='' b='' c='' m=''/>
  6. <Item a='' b='' c='' m=''/>
  7. <Item a='' b='' c='' m=''/>
  8. <Item a='' b='' c='' m=''/>
  9. <Item a='' b='' c='' m=''/>
  10. <Item a='' b='' c='' m=''/>
  11. <Item a='' b='' c='' m=''/>
  12. <Item a='A' b='' c='' m=''/>
  13. </Config>

测试结果:

1. 依 c 及 b 属性排序

2. b 属性改由大到小排序

  1. c,b
  2. a= b= c= m=
  3. a= b= c= m=
  4. a= b= c= m=
  5. a= b= c= m=
  6. a= b= c= m=
  7. a=A b= c= m=
  8. a= b= c= m=
  9. a= b= c= m=
  10. a= b= c= m=
  11. a= b= c= m=
  12. a= b= c= m=
  13.  
  14. c,-b
  15. a=A b= c= m=
  16. a= b= c= m=
  17. a= b= c= m=
  18. a= b= c= m=
  19. a= b= c= m=
  20. a= b= c= m=
  21. a= b= c= m=
  22. a= b= c= m=
  23. a= b= c= m=
  24. a= b= c= m=
  25. a= b= c= m=

测试程序:

  1. static void Main(string[] args)
  2. {
  3. var xml = new XmlDocument();
  4. xml.LoadXml(@"<Config>
  5. <Item a='1' b='5' c='9' m='9'/>
  6. <Item a='2' b='6' c='9' m='9'/>
  7. <Item a='3' b='7' c='9' m='9'/>
  8. <Item a='4' b='8' c='9' m='1'/>
  9. <Item a='5' b='9' c='9' m='1'/>
  10. <Item a='6' b='0' c='5' m='1'/>
  11. <Item a='7' b='1' c='5' m='1'/>
  12. <Item a='8' b='2' c='5' m='5'/>
  13. <Item a='9' b='3' c='5' m='5'/>
  14. <Item a='0' b='4' c='5' m='5'/>
  15. <Item a='A' b='5' c='5' m='5'/>
  16. </Config>");
  17.  
  18. var keepRunning = true;
  19. while (keepRunning)
  20. {
  21. var command = Console.ReadLine();
  22. switch (command)
  23. {
  24. case "/exit":
  25. case "/quit":
  26. keepRunning = false;
  27. break;
  28. default:
  29. try
  30. {
  31. foreach (var i in xml.SelectNodes("/Config/Item", command))
  32. {
  33. foreach (XmlAttribute a in i.Attributes)
  34. Console.Write("{0}={1} ", a.Name, a.Value);
  35. Console.WriteLine();
  36. }
  37. }
  38. catch (Exception ex)
  39. {
  40. Console.WriteLine(ex.Message);
  41. }
  42. break;
  43. }
  44. }
  45. }

核心程序:

  1. public static class XmlSorter
  2. {
  3. public static IEnumerable<XmlNode> SelectNodes(this XmlNode node, string xpath, string orderby)
  4. {
  5. var sorter = Sorter.Create(orderby);
  6. return
  7. sorter == null
  8. ? node.SelectNodes(xpath).AsEnumerable()
  9. : sorter.Sort(node.SelectNodes(xpath));
  10. }
  11. private static IEnumerable<XmlNode> AsEnumerable(this XmlNodeList list)
  12. {
  13. foreach (XmlNode i in list)
  14. yield return i;
  15. }
  16. private static string Attrib(this XmlNode node, string name, string defaultValue)
  17. {
  18. return (node.Attributes[name] == null)
  19. ? defaultValue
  20. : node.Attributes[name].Value;
  21. }
  22. class Sorter
  23. {
  24. public string Key { get; set; }
  25. public bool Descending { get; set; }
  26. public Sorter Next { get; set; }
  27. private IOrderedEnumerable<XmlNode> Sort(IOrderedEnumerable<XmlNode> list)
  28. {
  29. var flow = (Next != null ? 0x10 : 0x00) + (Descending ? 0x01 : 0x00);
  30. switch (flow)
  31. {
  32. case 0x11: return Next.Sort(list.ThenByDescending(o => o.Attrib(Key, "")));
  33. case 0x10: return Next.Sort(list.ThenBy(o => o.Attrib(Key, "")));
  34. case 0x01: return list.ThenByDescending(o => o.Attrib(Key, ""));
  35. case 0x00: return list.ThenBy(o => o.Attrib(Key, ""));
  36. }
  37. throw new Exception("!!!");
  38. }
  39. public IEnumerable<XmlNode> Sort(XmlNodeList nodes)
  40. {
  41. var flow = (Next != null ? 0x10 : 0x00) + (Descending ? 0x01 : 0x00);
  42. switch (flow)
  43. {
  44. case 0x11: return Next.Sort(nodes.AsEnumerable().OrderByDescending(o => o.Attrib(Key, "")));
  45. case 0x10: return Next.Sort(nodes.AsEnumerable().OrderBy(o => o.Attrib(Key, "")));
  46. case 0x01: return nodes.AsEnumerable().OrderByDescending(o => o.Attrib(Key, ""));
  47. case 0x00: return nodes.AsEnumerable().OrderBy(o => o.Attrib(Key, ""));
  48. }
  49. return null;
  50. }
  51. public static Sorter Create(string orderby)
  52. {
  53. if (string.IsNullOrEmpty(orderby)) return null;
  54. var fields = orderby.Split(',');
  55. var list = new List<Sorter>();
  56. foreach (var i in fields)
  57. {
  58. var s = i.Trim();
  59. var desc = s.StartsWith("-");
  60. var key = desc ? s.Substring() : s;
  61. if (string.IsNullOrEmpty(key)) continue;
  62. list.Add(new Sorter { Key = key, Descending = desc });
  63. }
  64. for (int i = ; i < list.Count; i++)
  65. list[i - ].Next = list[i];
  66. if (list.Count > ) return list[];
  67. return null;
  68. }
  69. }
  70. }

原谅链接:http://www.dotblogs.com.tw/cihsieh/archive/2013/11/26/131445.aspx

【转】为 XmlNode.SelectNodes 加上排序功能的更多相关文章

  1. redis(四)--简单实现Redis缓存中的排序功能

    在实现缓存排序功能之前,必须先明白这一功能的合理性.不妨思考一下,既然可以在数据库中排序,为什么还要把排序功能放在缓存中实现呢?这里简单总结了两个原因:首先,排序会增加数据库的负载,难以支撑高并发的应 ...

  2. 禁用datagridview中的自动排序功能

    把datagridview中的自动排序功能禁用自己收集的两种方法,看看吧①DataGridView中的Columns属性里面可以设置.进入"EditColumns"窗口后,在相应的 ...

  3. ListBox实现拖拽排序功能

    1.拖拽需要实现的事件包括: PreviewMouseLeftButtonDown LBoxSort_OnDrop 具体实现如下: private void LBoxSort_OnPreviewMou ...

  4. 简单实现Redis缓存中的排序功能

    1.在实现缓存排序功能之前,必须先明白这一功能的合理性.不妨思考一下,既然可以在数据库中排序,为什么还要把排序功能放在缓存中实现呢?这里简单总结了两个原因:首先,排序会增加数据库的负载,难以支撑高并发 ...

  5. Java实现中文字符串的排序功能

    package test; /** * * @Title 书的信息类 * @author LR * @version 1.0 * @since 2016-04-21 */ public class B ...

  6. MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能

    MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能 由于MYSQL没有提供类似ORACLE中OVER()这样丰富的分析函数. 所以在MYSQ ...

  7. nls_sort和nlssort 排序功能介绍

    nls_sort和nlssort 排序功能介绍 博客分类: oracle   ALTER SESSION SET NLS_SORT=''; 排序影响整个会话 Oracle9i之前,中文是按照二进制编码 ...

  8. [WPF]ListView点击列头排序功能实现

    [转]   [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...

  9. MVC5 Entity Framework学习参加排序、筛选和排序功能

    上一篇文章实现Student 基本的实体CRUD操作.本文将展示如何Students Index页添加排序.筛选和分页功能. 以下是排序完成时.经过筛选和分页功能截图,您可以在列标题点击排序. 1.为 ...

随机推荐

  1. python 网络编程(四)---UDP服务端客户端

    1.服务器端 UDP服务器建立与TCP相类似,具体比较如下: 补充下,第四步:不必使用listen还有accept函数. 具体代码如下:(设置socket选项省略) import socket fro ...

  2. java ee@ Myeclipse 2015 stable 1.0 完美破解方法

    Myeclipse 2015 stable 1.0 完美破解方法 破解步骤: 使用以前的注册机算号,版本选择Blue即可,后续可解锁Spring高级功能,即Bling的所有功能全部具备 1.1 进入m ...

  3. [读书笔记]了不起的node.js(二)

    这周做项目做得比较散(应该说一直都是这样),总结就依据不同情境双开吧-这篇记录的是关于node的学习总结,而下一篇是做项目学到的web前端的知识. 1.HTTP篇 node的HTTP模块在第一篇时接触 ...

  4. Intra Refresh of H264 encoder

    https://en.wikipedia.org/wiki/X264 x264 is able to use Periodic Intra Refresh instead of keyframes, ...

  5. 字符串匹配算法——KMP算法学习

    KMP算法是用来解决字符串的匹配问题的,即在字符串S中寻找字符串P.形式定义:假设存在长度为n的字符数组S[0...n-1],长度为m的字符数组P[0...m-1],是否存在i,使得SiSi+1... ...

  6. hdoj 2178 猜数字

    猜数字 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. nyoj 16 矩形嵌套

    矩形嵌套 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a& ...

  8. Altium Designer生成网表 导出网表【worldsing笔记】

    Design -> Netlist for project -> Protel

  9. 只允许input框输入数字,输入其他的键的时候,直接不显示的方法

    function numInteger(){ if((event.keyCode>=48 && event.keyCode<=57)  || (event.keyCode& ...

  10. SHH入门:Spring框架简介

    (1)Spring 七大模块 核心容器:核心容器提供Spring 框架的基本功能.核心容器的主要组件是 BeanFactory,它是工厂模式的实现.BeanFactory 使用控制反转 (IOC) 模 ...