开发者

problem in sort by date---> xml

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 creatin开发者_运维技巧g 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


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);


You could parse the dates... or you could actually take advantage of the fact that your date format is a sortable one. Try just specifying StringComparer.Ordinal. This won't spot any bad data, admittedly - but it will sort alphabetically, which will do what you want for dates.


Writing DateTime\TimeSpan to Xml files by ToString(...) will eventually fail.

In my experience, the best way to go is write the Ticks value (TimeSpan.Ticks) to the XML.

The only drawback is that if you view the XML it is quite hard to read the number, if this is important you can always add an attribute of the date in text format for easy reading.

Note that the reader should know how to handle DateTime format. If the reading system is Unix based then you might want to write FileTime instead of DateTime

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜