none
XML OrderBy Query RRS feed

  • Question

  • Hi,

    I'm using XML to LINQ and I have an XElement with some nodes that I would like to order.  Here is the data in the XElement:

    <Root>
      <User Name="Dan" Status="1" />
      <User Name="Brian" Status="0" />
      <User Name="David" Status="1" />
      <User Name="James" Status="0" />
      <User Name="Eric" Status="0" />
    </Root>

    I've tried the following but it does not change the order of the nodes:

    XElement xmlTree = new XElement("Root");

    // Do some work - Populate the tree

    xmlTree
         .Elements("User")
         .OrderByDescending(x => x.Attribute("Status").Value );

    I would like to sort by the Status attribute in descending order and then by the Name attribute in ascending order.  What am I doing wrong here?

    Thanks.

    Monday, October 5, 2009 11:44 PM

Answers

  • Do you want to create a new sorted tree? That can be done as follows:

                XElement root1 = XElement.Load(@"input.xml");
                XElement root2 =
                    new XElement(root1.Name,
                        root1
                        .Elements("User")
                        .OrderByDescending(u => (int)u.Attribute("Status"))
                        .ThenBy(u => (string)u.Attribute("Name")));
                root2.Save("output.xml");
    
    
    Or do you want to manipulate the existing tree? That can be done as follows:

                XElement root1 = XElement.Load("input.xml");
                List<XElement> users = root1.Elements("User").ToList();
                users.Remove();
                root1.Add(
                    users
                    .OrderByDescending(u => (int)u.Attribute("Status"))
                    .ThenBy(u => (string)u.Attribute("Name")));
                root1.Save("output.xml");

    MVP XML My blog
    • Marked as answer by David Donovan Tuesday, October 6, 2009 1:34 PM
    Tuesday, October 6, 2009 11:56 AM

All replies

  • Hi,

    I was able to run the following query in LINQ PAD and it gives correct result:

    var xmlTree = XElement.Parse (@"
    <Root>
      <User Name='Dan' Status='1' />
      <User Name='Brian' Status='0' />
      <User Name='David' Status='1' />
      <User Name='James' Status='0' />
      <User Name='Eric' Status='0' />
    </Root>");
    	
    xmlTree.Elements("User")
    .OrderByDescending(x => x.Attribute("Status").Value )
    .ThenBy(x => x.Attribute("Name").Value )
    .Dump();


    Can you tell what error are you getting?

    Regards
    Syed Mehroz Alam
    My Blog | My Articles
    Tuesday, October 6, 2009 4:52 AM
  • Do you want to create a new sorted tree? That can be done as follows:

                XElement root1 = XElement.Load(@"input.xml");
                XElement root2 =
                    new XElement(root1.Name,
                        root1
                        .Elements("User")
                        .OrderByDescending(u => (int)u.Attribute("Status"))
                        .ThenBy(u => (string)u.Attribute("Name")));
                root2.Save("output.xml");
    
    
    Or do you want to manipulate the existing tree? That can be done as follows:

                XElement root1 = XElement.Load("input.xml");
                List<XElement> users = root1.Elements("User").ToList();
                users.Remove();
                root1.Add(
                    users
                    .OrderByDescending(u => (int)u.Attribute("Status"))
                    .ThenBy(u => (string)u.Attribute("Name")));
                root1.Save("output.xml");

    MVP XML My blog
    • Marked as answer by David Donovan Tuesday, October 6, 2009 1:34 PM
    Tuesday, October 6, 2009 11:56 AM
  • Thanks Martin.  Worked like a charm.

    I didn't realize you had to re-create the data structure or remove/re-add the Elements again after doing the query on the fly.  I assumed it would know how to modify the existing data structure. 



    Syed, your query did not modify my tree.
    Tuesday, October 6, 2009 1:37 PM