如何使用C#检查XML元素是否按升序排列 [英] How to check whether XML elements are located in ascending order using C#

查看:81
本文介绍了如何使用C#检查XML元素是否按升序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查XML文件中的所有XML元素是否按升序排列。



我想通过名为ID的属性检查它们。





有没有办法这样做

怎么做。







I want to check whether all the XML element in the XML file are located in ascending order or not.

I want to check them through an attribute called ID.


Is there any way to do so
How to do so.



string[] g = new string[count];
        
            for (int i = 0; i < nodes.OfType<XPathNavigator>().ToList().Count; i++)
            {
                g[i] = nodes.OfType<XPathNavigator>().ToList()[i].GetAttribute("id", string.Empty);
            }

           

            int[] a = new int[count];
            for (int i = 0; i < count; i++)
            {

                a[i] = Convert.ToInt32(g[i]);
            }


            ArrayList missingNumbers = new ArrayList();
            for (int i = 0; i < count; i++)
            {
                if (!a.Contains(i))
                {
                    missingNumbers.Add(i);

                    MessageBox.Show(string.Format("Component {0} is Missing", i));
                }
            }

推荐答案

你可以让你的数组'a'排序。



you can make your array 'a' sorted.

List<int> a = g.ToList().Select(i=>int.Parse(i)).OrderBy(i=>i);
List<int> missings=new List<int>();

int idx=0; // a index
for(int i=0; i<a.Max(); i++) {
  if(i!=a[idx])
    Console.WriteLine("missing: {0}", i);
  else
    idx++;
}





然后使用此有序列表查找缺失。请注意,我没有运行该代码。它可能包含一些语法错误。但我认为这个想法很明确。



then use this ordered list to find missings. Be aware that I haven't run that code. It may contain some syntax error. But I think the idea is clear.


这篇关于如何使用C#检查XML元素是否按升序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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