Germstorm
Germstorm

Reputation: 9839

Order XmlNodeList based on an attribute

I have an XmlNodeList that contains packets (item) from the root of the XML example below. I want to sort the XmlNodeList based on the node's key attribute value. The sorting has to be very efficient, every millisecond counts.

Do you have any idea?

<root>
    <item key="1000000020">
        Content 20
    </item>
    <item key="1000000001">
        Content 1
    </item>
    ...
    <item key="1043245231">
        Content n
    </item>
</root>

Edit: I already have an XmlNodeList constructed from the items. I do not have access to the XmlDocument anymore, only the list of items.

Upvotes: 8

Views: 13594

Answers (3)

Yavuz
Yavuz

Reputation: 89

note: xml variable is string value

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

IEnumerable<XmlNode> rows = doc.SelectNodes("report/table/row").Cast<XmlNode>().OrderByDescending(r => Convert.ToDecimal(r.Attributes["conversions"].Value));

Upvotes: 6

Germstorm
Germstorm

Reputation: 9839

I solved the problem in a very non-elegant way:

  • I iterated my XmlNodeList
  • During iteration I extracted the timestamps
  • After extracting a timestamp I added the timestamp-XmlElement to a SortedDictionary
  • Converted the SortedDictionary to list (sortedKeys = sortedByDateDisctionary.Keys.ToList();)
  • If the nodes need to be sorted Descending then sortedKeys.Reverse();
  • Then the nodes can be accessed by the sorted keys

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94645

You should try Linq to XML.

 XDocument doc = XDocument.Load(file);

   var nodeList = from ele in doc.Descendants("item")
                   orderby int.Parse(ele.Attribute("key").Value)
                   select ele;

You may try XPathNavigator and XPathExpression.

 //I presume that variable xNodeList contains XmlNodeList.
  XPathNavigator nav=xNodeList.Item(0).OwnerDocument.CreateNavigator();
  XPathExpression exp = nav.Compile("root/item");


  exp.AddSort("@key", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number );

  foreach (XPathNavigator t in nav.Select(exp))
  {
    Console.WriteLine(t.OuterXml );
   }

Upvotes: 3

Related Questions