Hey everyone,

I have an Xml file in which I want to compare elements;

<Students>
    <Student id = "a">
      <Name>*** </Name>
      <Age>17 </Age>
    </Student>
 </Students>

<Students>
    <Student id = "b">
      <Name>*** </Name>
      <Age>17</Age>
    </Student>
 </Students>

if student "a" is of the same age as "b" I want to display a message. So haw can I compare these values while reading the file. So far I've used

foreach (XmlNode node in StudentList)
{
}

to read each Xml element. Is there a way to do it without having to store them?

How else would you? I mean, are you looking to load each "Student" into some sort of object, and then be able to say something like:

if (firstPerson.Age == secondPerson.Age)
{
    ...
}

If so, you will still have to process the XML to load the objects.

How else would you? I mean, are you looking to load each "Student" into some sort of object, and then be able to say something like:

if (firstPerson.Age == secondPerson.Age)
{
    ...
}

If so, you will still have to process the XML to load the objects.

Yes, I am using a student class, but how do I call the previous object( firstPerson) ??

First of all your XML should look more like this...

<Students>
      <Student id = "a">
      <Name>*** </Name>
      <Age>17 </Age>
      </Student>
      <Student id = "b">
      <Name>*** </Name>
      <Age>17</Age>
      </Student>
    </Students>

... because Student is the iterating type of Node.

The next Part is important. Create yourself an Object named Student!

class Student
    {
      public string ID { get; set; }
      public string Name { get; set; }
      public int Age { get; set; }

      /// <summary>
      /// Returns:
      ///   0 for none
      ///   1 for same age
      ///   2 for same name
      ///   4 for same ID
      /// 
      ///   3 for same age & name
      ///   5 for same ID and age
      ///   6 for same ID and name
      ///   7 for same everything
      /// </summary>
      public int CompareTo(Student otherStudent)
      {
        var r = 0;
        if (this.Age == otherStudent.Age) r = r + 1;
        if (this.Name == otherStudent.Name) r = r + 2;
        if (this.ID == otherStudent.ID) r = r + 4;
        return r;
      }
    }

Read your XML into a List of those and then compare them with the method I gave you! I did it without the comparer Interface because it' kind of lame!

Good look dude ! Any questions? your up!

Hey thanx for ur help....it really helped me a lot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.