Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 801541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:27:28+00:00 2026-05-14T23:27:28+00:00

Can’t get list sorted by date. How to show data ordered by date? XDocument

  • 0

Can’t get list sorted by date. How to show data ordered by date?

XDocument doc = XDocument.Load(Server.MapPath("file.xml"));
IEnumerable<XElement> items = from item in doc.Descendants("item")
                                      orderby Convert.ToDateTime(item.Attribute("lastChanges").Value) descending
                                      where item.Attribute("x").Value == 1
                                      select item;

        Repeater1.DataSource = items;
        Repeater1.DataBind();

Xml file looks like this:

<root>
  <items>
    <item id="1" lastChanges="15-05-2010" x="0" />
    <item id="2" lastChanges="16-05-2010" x="1" />
    <item id="3" lastChanges="17-05-2010" x="1" />
  </items>
</root>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:27:28+00:00Added an answer on May 14, 2026 at 11:27 pm

    I had to make a couple of changes to get the sample code to compile and sort as desired:

    var formatter = new DateTimeFormatInfo
        {
            ShortDatePattern = "dd/MM/yyyy"
        };
    var items = from item in doc.Descendants("item")
                orderby Convert.ToDateTime(item.Attribute("lastChanges").Value, formatter) descending
                where item.Attribute("x").Value == "1"
                select item;
    

    The main thing was to provide an IFormatProvider so that the system could correctly parse the dates.

    Output:

    <item id="3" lastChanges="17-05-2010" x="1" />
    <item id="2" lastChanges="16-05-2010" x="1" />
    

    If you separate the concern of filtering and ordering the data into a testable class you should be able verify that ordering works correctly and focus your problem search elsewhere.

    public class DataSource
    {
        private readonly XDocument _doc;
    
        public DataSource(XDocument doc)
        {
            _doc = doc;
        }
    
        public IEnumerable<XElement> GetSortedFilteredElements()
        {
            var formatter = new DateTimeFormatInfo
                {
                    ShortDatePattern = "dd/MM/yyyy"
                };
            var items = from item in _doc.Descendants("item")
                        orderby Convert.ToDateTime(item.Attribute("lastChanges").Value, formatter) descending
                        where item.Attribute("x").Value == "1"
                        select item;
            return items;
        }
    }
    

    usage:

    Repeater1.DataSource =
      new DataSource(XDocument.Load(Server.MapPath("file.xml")))
        .GetSortedFilteredElements();
    

    The next thing to look at would be, as @david-b suggested, whether the Repeater is reordering the inputs because the stringified XmlNode values would definitely come out in a different order if sorted as strings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can find why i get this error can someone help? package Android.data; public class
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can any one tell, how to get the result of LINQ query contains group
Can we put functions to determine the condition for our list comprehensions. Here is
can someone show me the regex for this preg_match. I want to make sure
Can't seem to get the Back Button to appear in a UINavigationController flow. I
Can I convert a pdf to pcl file with ghostscript? I'm using Ghostscript 9.01
Can i get the source code for a WAMP stack installer somewhere? Any help
Can you cast a List<int> to List<string> somehow? I know I could loop through
Can someone tell me how to get an UIView controller that it was created

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.