【转】为 XmlNode.SelectNodes 加上排序功能
测试资料:
<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 加上排序功能的更多相关文章
- redis(四)--简单实现Redis缓存中的排序功能
在实现缓存排序功能之前,必须先明白这一功能的合理性.不妨思考一下,既然可以在数据库中排序,为什么还要把排序功能放在缓存中实现呢?这里简单总结了两个原因:首先,排序会增加数据库的负载,难以支撑高并发的应 ...
- 禁用datagridview中的自动排序功能
把datagridview中的自动排序功能禁用自己收集的两种方法,看看吧①DataGridView中的Columns属性里面可以设置.进入"EditColumns"窗口后,在相应的 ...
- ListBox实现拖拽排序功能
1.拖拽需要实现的事件包括: PreviewMouseLeftButtonDown LBoxSort_OnDrop 具体实现如下: private void LBoxSort_OnPreviewMou ...
- 简单实现Redis缓存中的排序功能
1.在实现缓存排序功能之前,必须先明白这一功能的合理性.不妨思考一下,既然可以在数据库中排序,为什么还要把排序功能放在缓存中实现呢?这里简单总结了两个原因:首先,排序会增加数据库的负载,难以支撑高并发 ...
- Java实现中文字符串的排序功能
package test; /** * * @Title 书的信息类 * @author LR * @version 1.0 * @since 2016-04-21 */ public class B ...
- MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能
MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能 由于MYSQL没有提供类似ORACLE中OVER()这样丰富的分析函数. 所以在MYSQ ...
- nls_sort和nlssort 排序功能介绍
nls_sort和nlssort 排序功能介绍 博客分类: oracle ALTER SESSION SET NLS_SORT=''; 排序影响整个会话 Oracle9i之前,中文是按照二进制编码 ...
- [WPF]ListView点击列头排序功能实现
[转] [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...
- MVC5 Entity Framework学习参加排序、筛选和排序功能
上一篇文章实现Student 基本的实体CRUD操作.本文将展示如何Students Index页添加排序.筛选和分页功能. 以下是排序完成时.经过筛选和分页功能截图,您可以在列标题点击排序. 1.为 ...
随机推荐
- good bye 2015 B - New Year and Old Property
题意:统计在n,m之间的数的二进制表示形式只有一个零的数目. 位运算模拟+dfs #include<iostream> #include<string> #include< ...
- c++11信号量实现
c++11中有 mutex (互斥量),有 condition_variable (条件变量),并没有 semaphore (信号量).信号量,操作系统中一般都有提,后来 google 说可以使用 m ...
- ACM2026
/* 首字母变大写 Problem Description 输入一个英文句子,将每个单词的第一个字母改成大写字母. Input 输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句 ...
- Tomcat普通用户部署教程(生产服务器)
1.环境准备 JDK安装 解压 tar xf tomcat-xx.tar.gz -C /data/soft cd /data/soft 重命名 mv tomcat-xx tom ...
- SGU-495 Kids and Prizes 概率DP
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:有n个盒子,每个盒子里面放了一个奖品,m个人轮流去选择盒子,如果盒子里面 ...
- 创建本地CM 离线服务器
一.包管理工具及CentOS的yum 1.包管理工具如何发现可以用的包 包管理工具依赖一系列软件源,工具下载源的信息存储在配置文件中,其位置随某包管理工具不同而变化 使用yum的RedHat/Cent ...
- 宿舍局域网与Internet连接
写在前面的话 一般情况下,大多数组建了校园网的学校都为学生宿舍提供了连接Internet接口,所以只需要通过网线将宿舍网的集线器与校园网提供的接口进行连接即可接入Internet.宿舍网接入Inter ...
- nyoj 14 会场安排问题
会场安排问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...
- [iOS基础控件 - 1] UI概念
A. UIView 1.概念 属于UIKit框架 屏幕上能看得见摸得着的东西就是UIView,比如屏幕上的按钮.文字.图片 翻译为:视图/控件/组件 UIBut ...
- Java快捷键
Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...