Hi,

I have an XML file that I am trying to parse and then sort into an re-ordered list.

Currently the XML file is ordered via date ascending i.e. oldest item first, newest last.

I know how to parse an XML file using 'XMLNodeList - GetElementsByTagName' but what would be the best (simplest) way after retreiving the required Attributes, to then sort them as I need?

I have thought about loading the strings into a hidden ListBox on my form and ordering that way, also I have search and found reference to 'ToList', I guess arrays could be used also.

Any ideas would be a help.

Regards..,

MT

Hi,

I have an XML file that I am trying to parse and then sort into an re-ordered list.

Currently the XML file is ordered via date ascending i.e. oldest item first, newest last.

I know how to parse an XML file using 'XMLNodeList - GetElementsByTagName' but what would be the best (simplest) way after retreiving the required Attributes, to then sort them as I need?

I have thought about loading the strings into a hidden ListBox on my form and ordering that way, also I have search and found reference to 'ToList', I guess arrays could be used also.

Any ideas would be a help.

Regards..,

MT

To make things a little more clear, here is a simplified version of the XML file that I need parsed & sorted:-

<?xml version="1.0"?>
<products>
  <product id="570813738" name="..." storeUri="...">...</product>
  <product id="570813739" name="..." storeUri="...">...</product>
  <product id="570813740" name="..." storeUri="...">...</product>
  <product id="570813741" name="..." storeUri="...">...</product>
  <product id="570813742" name="..." storeUri="...">...</product>
  <product id="570813743" name="..." storeUri="...">...</product>
  <product id="570813744" name="..." storeUri="...">...</product>
</products>

What I would like to achieve is for the XML file to be parsed by 'id', 'name' & 'storeUri' and then sorted by 'id' from last number to first number, decending I guess.

Oh and then saved as a new XML file.

Hope this helps.

Regards..,

MT

Edited 5 Years Ago by Cap'nKirk: n/a

If you convert your XmlNodeList to a generic list you could use Linq to sort it.
Something like this.

var xmlDoc = new XmlDocument();
// load doc here
var tags = xmlDoc.GetElementsByTagName("TagName");
var tagList = new List<XmlNode>(tags.Cast<XmlNode>());
tagList.Sort((a, b) => System.Convert.ToInt64(a["id"]).CompareTo(System.Convert.ToInt64(b["id"])));

Edited 5 Years Ago by nick.crane: Changed ToDateTime to ToInt64

If you convert your XmlNodeList to a generic list you could use Linq to sort it.
Something like this.

var xmlDoc = new XmlDocument();
// load doc here
var tags = xmlDoc.GetElementsByTagName("TagName");
var tagList = new List<XmlNode>(tags.Cast<XmlNode>());
tagList.Sort((a, b) => System.Convert.ToInt64(a["id"]).CompareTo(System.Convert.ToInt64(b["id"])));

Hi Nick,

thanks for replying.

I am trully stuck with this, I can't seem to get anything to work as it should.

I have absolutely no idea of where to go next.

Tried utilizing the snippet you posted but to no avail.

Would it possible for you to show me in more depth how to sort my above posted sample xml file by 'id' descending and then save it as a new xml file.

This would be greatly appreciated.

Kind regards..,

MT

Hi Nick,

thanks for replying.

I am trully stuck with this, I can't seem to get anything to work as it should.

I have absolutely no idea of where to go next.

Tried utilizing the snippet you posted but to no avail.

Would it possible for you to show me in more depth how to sort my above posted sample xml file by 'id' descending and then save it as a new xml file.

This would be greatly appreciated.

Kind regards..,

MT

Right, I think I've got it, seems to work.

A lot of messing around has produced this:-

private void button1_Click(object sender, EventArgs e)
        {

          var xDoc = XDocument.Load(System.IO.Path.Combine(Application.StartupPath, "shortv.xml"));

          xDoc.Root.ReplaceAll(from p in xDoc.Root.Elements("product")
                                orderby int.Parse(p.Attribute("id").Value) descending
                                select p);

          xDoc.Save(System.IO.Path.Combine(Application.StartupPath, "shortv2.xml"));
        }

No idea how I stumbled on this but there you go.

Kind regards..,

MT

Comments
Well done.

Well done.
That is much slicker than the crappy solution I was working on.

I have given myself a little pat on the back.

I don't quite understand fully how it all works, I would love to though, in time I guess.

Anyhow, thanks buddy for the input.

All the best.., :)

MT

This question has already been answered. Start a new discussion instead.