测试资料:

<Config>
<Item a='' b='' c='' m=''/>
<Item a='' b='' c='' m=''/>
<Item a='' b='' c='' m=''/>
<Item a='' b='' c='' m=''/>
<Item a='' b='' c='' m=''/>
<Item a='' b='' c='' m=''/>
<Item a='' b='' c='' m=''/>
<Item a='' b='' c='' m=''/>
<Item a='' b='' c='' m=''/>
<Item a='' b='' c='' m=''/>
<Item a='A' b='' c='' m=''/>
</Config>

测试结果:

1. 依 c 及 b 属性排序

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

c,b
a= b= c= m=
a= b= c= m=
a= b= c= m=
a= b= c= m=
a= b= c= m=
a=A b= c= m=
a= b= c= m=
a= b= c= m=
a= b= c= m=
a= b= c= m=
a= b= c= m= c,-b
a=A b= c= m=
a= b= c= m=
a= b= c= m=
a= b= c= m=
a= b= c= m=
a= b= c= m=
a= b= c= m=
a= b= c= m=
a= b= c= m=
a= b= c= m=
a= b= c= m=

测试程序:

static void Main(string[] args)
{
var xml = new XmlDocument();
xml.LoadXml(@"<Config>
<Item a='1' b='5' c='9' m='9'/>
<Item a='2' b='6' c='9' m='9'/>
<Item a='3' b='7' c='9' m='9'/>
<Item a='4' b='8' c='9' m='1'/>
<Item a='5' b='9' c='9' m='1'/>
<Item a='6' b='0' c='5' m='1'/>
<Item a='7' b='1' c='5' m='1'/>
<Item a='8' b='2' c='5' m='5'/>
<Item a='9' b='3' c='5' m='5'/>
<Item a='0' b='4' c='5' m='5'/>
<Item a='A' b='5' c='5' m='5'/>
</Config>"); var keepRunning = true;
while (keepRunning)
{
var command = Console.ReadLine();
switch (command)
{
case "/exit":
case "/quit":
keepRunning = false;
break;
default:
try
{
foreach (var i in xml.SelectNodes("/Config/Item", command))
{
foreach (XmlAttribute a in i.Attributes)
Console.Write("{0}={1} ", a.Name, a.Value);
Console.WriteLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
break;
}
}
}

核心程序:

public static class XmlSorter
{
public static IEnumerable<XmlNode> SelectNodes(this XmlNode node, string xpath, string orderby)
{
var sorter = Sorter.Create(orderby);
return
sorter == null
? node.SelectNodes(xpath).AsEnumerable()
: sorter.Sort(node.SelectNodes(xpath));
}
private static IEnumerable<XmlNode> AsEnumerable(this XmlNodeList list)
{
foreach (XmlNode i in list)
yield return i;
}
private static string Attrib(this XmlNode node, string name, string defaultValue)
{
return (node.Attributes[name] == null)
? defaultValue
: node.Attributes[name].Value;
}
class Sorter
{
public string Key { get; set; }
public bool Descending { get; set; }
public Sorter Next { get; set; }
private IOrderedEnumerable<XmlNode> Sort(IOrderedEnumerable<XmlNode> list)
{
var flow = (Next != null ? 0x10 : 0x00) + (Descending ? 0x01 : 0x00);
switch (flow)
{
case 0x11: return Next.Sort(list.ThenByDescending(o => o.Attrib(Key, "")));
case 0x10: return Next.Sort(list.ThenBy(o => o.Attrib(Key, "")));
case 0x01: return list.ThenByDescending(o => o.Attrib(Key, ""));
case 0x00: return list.ThenBy(o => o.Attrib(Key, ""));
}
throw new Exception("!!!");
}
public IEnumerable<XmlNode> Sort(XmlNodeList nodes)
{
var flow = (Next != null ? 0x10 : 0x00) + (Descending ? 0x01 : 0x00);
switch (flow)
{
case 0x11: return Next.Sort(nodes.AsEnumerable().OrderByDescending(o => o.Attrib(Key, "")));
case 0x10: return Next.Sort(nodes.AsEnumerable().OrderBy(o => o.Attrib(Key, "")));
case 0x01: return nodes.AsEnumerable().OrderByDescending(o => o.Attrib(Key, ""));
case 0x00: return nodes.AsEnumerable().OrderBy(o => o.Attrib(Key, ""));
}
return null;
}
public static Sorter Create(string orderby)
{
if (string.IsNullOrEmpty(orderby)) return null;
var fields = orderby.Split(',');
var list = new List<Sorter>();
foreach (var i in fields)
{
var s = i.Trim();
var desc = s.StartsWith("-");
var key = desc ? s.Substring() : s;
if (string.IsNullOrEmpty(key)) continue;
list.Add(new Sorter { Key = key, Descending = desc });
}
for (int i = ; i < list.Count; i++)
list[i - ].Next = list[i];
if (list.Count > ) return list[];
return null;
}
}
}

原谅链接: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. 50道经典的JAVA编程题(31-35)

    50道经典的JAVA编程题(31-35),今天考完了java,在前篇博客里面贴出了题了,见:<今天考试的JAVA编程题>.考完了也轻松了,下个星期一还考微机原理呢,啥都不会,估计今天就做到 ...

  2. leetcode@ [318] Maximum Product of Word Lengths (Bit Manipulations)

    https://leetcode.com/problems/maximum-product-of-word-lengths/ Given a string array words, find the ...

  3. nyoj 115 城市平乱

    城市平乱 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 南将军统领着N个部队,这N个部队分别驻扎在N个不同的城市. 他在用这N个部队维护着M个城市的治安,这M个城市 ...

  4. 转载Expression Tree揭秘

    概述 在.NET Framework 3.5中提供了LINQ 支持后,LINQ就以其强大而优雅的编程方式赢得了开发人员的喜爱,而各种LINQ Provider更是满天飞,如LINQ to NHiber ...

  5. MSSQLSERVER数据库- SQL删除重复数据的五种方式

    删除重复的数据,在平时的工作中还是会和碰到的,感觉挺有用,从网上摘录的,记在这里,以备需要时查阅 --方法一,IN方式,适合2000/2005/2008,6728 毫秒 DELETE [student ...

  6. iOS 开发查看应用的沙盒文件

    在iOS开发中,常常需要将一些信息保存到本地,比如说用户的一些搜索历史等.那么,如何查看所保存的文件呢? 这里介绍两种途径来查看应用的沙盒文件. 方法一:通过Xcode来查看,步骤如下: (1): X ...

  7. 【Cocos2d-X开发学习笔记】第09期:渲染框架之菜单类(CCMenu)的使用

    本系列学习教程使用的是cocos2d-x-2.1.4(最新版为3.0alpha0-pre) ,PC开发环境Windows7,C++开发环境VS2010    一.菜单项(CCMenuItem) 菜单项 ...

  8. cocos2d-x 判断点击命中坐标的几种方法

    转自:http://www.cnblogs.com/jiackyan/archive/2013/04/14/3019893.html //重载 virtual bool ccTouchBegan(CC ...

  9. 创建性能监视器(logman)

    在本地计算机上抓取性能信息 Logman.exe create counter Perf-1Second -f bincirc -max 500 -c "\Processor(*)\% Pr ...

  10. sgu 101 无向图有双重边的欧拉路径

    #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> ...