如何基于属性“名称"对xml进行排序? [英] How to sort an xml Based on Attribute "Name"

查看:119
本文介绍了如何基于属性“名称"对xml进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?xml version="1.0" encoding="utf-8" ?>
<Employees>
 <Employee EmpId="1'" Name="Sam" />
 <Employee EmpId="2" Name="Lucy"/>
 <Employee EmpId="3" Name="Kate"/>
 <Employee EmpId="4" Name="Chris"/>
 <Employee EmpId="5" Name="David"/>
 <Employee EmpId="6" Name="Zolar"/>
 <Employee EmpId="7" Name="Shannon"/>
</Employees>



大家好,
根据所选的名称属性,我必须按排序顺序显示xml怎么办?

在此先感谢



Hello All ,
Based on Name Attribute selected i have to display the xml in sorted order how shall i do this?

Thanks In Advance

推荐答案



您可以尝试 XPathNavigator XPathExpression


使用系统;
使用System.Xml;
使用System.Xml.XPath;


命名空间TestApp_Console_3
{
课程计划
{
静态void Main(string [] args)
{
XmlDocument doc =新的XmlDocument();
doc.Load("Test.xml");
XPathNavigator导航= doc.CreateNavigator();
XPathExpression exp = nav.Compile("Employees/Employee");
exp.AddSort("@ Name",XmlSortOrder.Ascending,XmlCaseOrder.None,",XmlDataType.Text);

foreach(nav.Select(exp)中的XPathNavigator t)
{
Console.WriteLine(t.OuterXml);
}
Console.ReadLine();
}
}
}
Hi,

You may try XPathNavigator and XPathExpression


using System;
using System.Xml;
using System.Xml.XPath;


namespace TestApp_Console_3
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("Test.xml");
XPathNavigator nav = doc.CreateNavigator();
XPathExpression exp = nav.Compile("Employees/Employee");
exp.AddSort("@Name", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text);

foreach (XPathNavigator t in nav.Select(exp))
{
Console.WriteLine(t.OuterXml);
}
Console.ReadLine();
}
}
}


这篇关于如何基于属性“名称"对xml进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆