1

I searched the web for this, but I am not able to find any related solution.

I have following XML

  <Table>
    <Entity>Employee</Entity>
    <EntityID>2786</EntityID>
    <Goal>
      <GoalId>31931</GoalId>
      <FilterData>LastModifiedOn¥2014-03-20T18:11:01.0000000+05:30ÆActiveTaskCount¥0</FilterData>
    </Goal>
    <Goal>
      <GoalId>31932</GoalId>
      <FilterData>LastModifiedOn¥2014-03-22T15:26:09.0000000+05:30ÆActiveTaskCount¥0</FilterData>
    </Goal>
    <Goal>
      <GoalId>31932</GoalId>
      <FilterData>LastModifiedOn¥2014-03-22T09:25:00.0000000+05:30ÆActiveTaskCount¥0</FilterData>
    </Goal>
  </Table> 

From above XML when I read the data I got 2 separate DataTables; 1 for Employees and another one for related Goals.

What I needed is I want to sort all the Goals related to an employee with respect to LastModifiedOn from FilterData node.

NOTE: I am getting the LastModifiedOn value by split node value like this

nodevalue.Split('Æ')[0].Split('¥')[1]

Right now I am using System.XML namespace for doing operations. I also looked at LINQ TO XML but I am unable to make it work.

I am getting the XMLNodelist by following code

XmlNodeList GoalNodesList = doc.DocumentElement.SelectNodes("/NewDataSet/Table[Entity='Employee' and EntityID='" + EntityId + "']/Goal");

Here I want the Sorted Goals (with respect to LastModifiedOn).

I also looked at some useful links but didn't get any idea so far

  1. Sorting XML nodes based on DateTime attribute C#, XPath
  2. XMLdocument Sort

I am ready to convert the code into LINQ TO XML but needed a brief example.

Community
  • 1
  • 1
nrsharma
  • 2,532
  • 3
  • 20
  • 36

1 Answers1

0

One thing you could try is creating a collection of key value pairs where key is the goal and value is the date. You could then override the CompareTo method by comparing the dates and then you could just use [collection name].Sort(). Hope this helps

Maxqueue
  • 2,194
  • 2
  • 23
  • 55