Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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.



C#
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));
                }
            }
Posted
Updated 17-Mar-14 22:36pm
v3
Comments
Vedat Ozan Oner 18-Mar-14 3:39am    
1- read xml file.
2- until end of xml, check if it is ordered by ID or not.
if you stuck at some point of your code, you can ask it here again.
KUMAR619 18-Mar-14 4:25am    
As you've told I've collected the Ids of all element in node.

I have found out the missing numbers but I could make it fine when the id are not in order.


If id=4,6,5
Then Its showing nothing missed.

But it should point out then element which is not present.

Thats why I want to check whether the elements are arranged in ascending order
Vedat Ozan Oner 18-Mar-14 4:31am    
can you share that part of the code?
KUMAR619 18-Mar-14 4:37am    
I've updated my code.

Please refer it and get me a code

1 solution

you can make your array 'a' sorted.

C#
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.
 
Share this answer
 
v2
Comments
KUMAR619 18-Mar-14 5:04am    
You're right but these numbers are collected from XML elements as string.

So Even I can't order existing XML node and elements.

My question is if I have mixed numbers in the array it will not show missing elements because the element exists now my question is that report that mixed number also

In the above code

!a.Contains(i)

is there any alternate code for this statement
Vedat Ozan Oner 18-Mar-14 5:11am    
that part of your code should work as much as I understand. But I'm updating my solution to find missing numbers in a series.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900