如何使用C#基于其ID对Xml文件节点进行排序 [英] How to sort an Xml file node base on its ID using C#

查看:102
本文介绍了如何使用C#基于其ID对Xml文件节点进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C#基于其ID对XMl文件节点进行排序。



我知道如何编辑XML文件中元素的属性。



但是如何按ID分类整个XML文件。



有没有办法这样做。 />


如果有,那么任何人都可以给我一个代码片段来解决这个问题

How to sort an XMl file node base on its ID using C#.

I know how to edit attributes of an element in XML file.

But how to sort the whole XML file by its ID.

Is there any way to do so.

If so then can anyone get me a code snippet to solve this problem

推荐答案

XmlDocument myXmlDoc = new XmlDocument ();



myXMLNode = myXmlDoc.SelectSingleNode(record / Page1);

page1是节点,

记录是根,



你输入你自己的根和节点来排序



string Cubs_Account_Number = myXMLNode [CUBS_Account_Number]。InnerText;



你可以这样排序



样本xml file ::



XmlDocument myXmlDoc = new XmlDocument();

myXMLNode = myXmlDoc.SelectSingleNode("record/Page1");
page1 is the node,
record is the root,

you enter your own root and node to sort

string Cubs_Account_Number = myXMLNode["CUBS_Account_Number"].InnerText;

you can sort like that

Sample xml file::

<record>
<page1>

<cubs_account_number>123456789</cubs_account_number>
</page1>

</record>


<Root a="1">
   <I aa="1" b="2">
   <I aa=5" b="2">
   <I aa="3" b="2">
   <I aa="4" b="2">
</Root>


var xml= xDoc.Element(&quot;Root&quot;)
                .Elements(&quot;I&quot;)
                .OrderByDescending(s =&gt; (int) s.Attribute(&quot;aa&quot;));


这篇关于如何使用C#基于其ID对Xml文件节点进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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