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

  • SEARCH
  • Home
  • 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 517397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:49:47+00:00 2026-05-13T07:49:47+00:00

I have a xml and I want to sort it by date.my code till

  • 0

I have a xml and I want to sort it by date.my code till now is this

XmlDocument doc = new XmlDocument();

    doc.Load("/ABC.xml");

    XPathNavigator nav = doc.CreateNavigator();

      //doing some filtering and creating xpath expression based on that
     XPathExpression expression = nav.Compile(xpath);
   XPathExpression sortexpression = nav.Compile("/categories/category/date/text()");
   DateComparer dc = new DateComparer();

  //expression.AddSort("sortexpression", dc);

Edited: I have changed this to

expression.AddSort(sortexpression,dc);

now there is no error but sortin is not working properly and when I while I came to know that in datecomparer class only first date of my xml is going and because of that there is no sorting going on. But why only my first date of xml is going into datecomparer.Any Idea where I am wrong??

    XPathNodeIterator iterator = nav.Select(expression);

later on binding this iterator with my repeater

public class DateComparer : IComparer
    {

        public DateComparer() { }
        public int Compare(object date1, object date2)
        {

            DateTime d1 = Convert.ToDateTime(date1);
            DateTime d2 = Convert.ToDateTime(date2);
                           return d1.CompareTo(d2);
        }

    }

My xml is like this

<categories>
 <category>
  <title>ABCD<title>
  <date>2002-01-01<date>
 <category>
<categories>

Date i m givin is in yyyy-mm-dd format. I have tried date in others format like mm/dd/yyyy and dd/mm/yyyy also.

//but when i m executing this code I m getting the error ” string was not recognized as a valid Datetime”.Any idea what is the problem in my code?

Edited:No error see above

  • 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-13T07:49:47+00:00Added an answer on May 13, 2026 at 7:49 am

    Assuming that the dates are read as strings you’re going to need to provide an IFormatProvider to your Convert.ToDateTime method as the date you are using is non-standard. If you don’t then the conversion method will use the current culture.

    UK English would be day-month-year and US English would be month-day-year for example.

    For year-month-day you may even need a custom format provider, or you could use DateTime.ParseExact:

    DateTime dt = DateTime.ParseExact (s, "yyyy-MM-dd", CultureInfo.InvariantCulture);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this xml file, and now i will want to add new entries
Hello i have this XML and i want to sort all the MESSAGE/DATA/TRANSACTIONS/TRANSACTION With
I have an XML documment that I want to load for the iPhone, do
I have an XML file that I want to sort by an attribute. The
I have an XML file whose contents I want to sort by document order
I have some XML like this: <root> <do-not-sort> <z/> <y/> </do-not-sort> <sortable> <e f=fog
I am using this code to retrieve the values I want from XML: IEnumerable<ForewordReview>
I have an XML file that contains price value.I want to sort my table
I have XML in the following form that I want to parse with PHP
I have following xml and I want to fetch the value of node which

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.