我如何排序的属性一个XDocument? [英] How can I sort an XDocument by attribute?

查看:153
本文介绍了我如何排序的属性一个XDocument?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些XML

<Users>
    <User Name="Z"/>
    <User Name="D"/>
    <User Name="A"/>
</User>



我要进行排序,通过的名称的。我加载使用的XDocument 的XML。我怎样才能查看XML按名称排序?

I want to sort that by Name. I load that xml using XDocument. How can I view that xml sorted by Name?

推荐答案

您可以排序使用LINQ to XML,如果XmlDocument的情况并非如此

You can sort using LINQ to Xml, if XmlDocument is not the case

XDocument input = XDocument.Load(@"input.xml");
XDocument output = new XDocument(
    new XElement("Users",
        from node in input.Root.Elements()
        orderby node.Attribute("Name").Value descending
        select node));

这篇关于我如何排序的属性一个XDocument?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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