Sort Xpath from an XML Document

I have an Xml document and it contains some date data, for example:

<Message> <messagetext>Testing purpose only</messagetext> <date>05.02.2010</date> </Message> 

I want them to be sorted using the XPath key, how can I get if anyone knows about pls help me

thanks everyone

+4
source share
4 answers

You can do this using LINQ to XML as follows. I assume that you are using the MM.dd.yyyy format, but this can be easily changed if you want:

 using System; using System.Linq; using System.Xml.Linq; public class Message { public string Text { get; set; } public DateTime Date { get; set; } } class Program { static void Main(string[] args) { XDocument doc = XDocument.Load("input.xml"); var messages = doc.Descendants("Message") .Select(element => new Message { Text = element.Element("messagetext").Value, Date = DateTime.ParseExact(element.Element("date").Value, "MM.dd.yyyy", null) }).OrderBy(message => message.Date); foreach (Message message in messages) { Console.WriteLine("{0} : {1}", message.Date, message.Text); } } } 

Results:

 02-05-2010 00:00:00 : Test1 17-05-2010 00:00:00 : Test2 22-05-2010 00:00:00 : Test3 

Test data I used:

 <xml> <Message> <messagetext>Test1</messagetext> <date>05.02.2010</date> </Message> <Message> <messagetext>Test3</messagetext> <date>05.22.2010</date> </Message> <Message> <messagetext>Test2</messagetext> <date>05.17.2010</date> </Message> </xml> 
+1
source

The XPathExpression class allows you to add sorting options. Here is sample code that should work with .NET 2.0:

 XPathDocument doc = new XPathDocument(@"..\..\XMLFile1.xml"); XPathNavigator nav = doc.CreateNavigator(); XPathExpression exp = nav.Compile("Messages/Message"); exp.AddSort( "number(concat(substring(date, 7), substring(date, 4, 2), substring(date, 1, 2)))", XmlSortOrder.Descending, XmlCaseOrder.None, null, XmlDataType.Number ); foreach (XPathNavigator msg in nav.Select(exp)) { Console.WriteLine( "{0}: {1}", msg.SelectSingleNode("date").Value, msg.SelectSingleNode("messagetext").Value ); } 

When using XMLFile1.xml

 <Messages> <Message> <messagetext>Message 2</messagetext> <date>04.02.2010</date> </Message> <Message> <messagetext>Message 1</messagetext> <date>05.02.2010</date> </Message> <Message> <messagetext>Message 3</messagetext> <date>05.02.2009</date> </Message> </Messages> 

conclusion

 05.02.2010: Message 1 04.02.2010: Message 2 05.02.2009: Message 3 

The expected date format is ddmmyyyy, but you can change these substring expressions as needed if you want mmddyyyy.

+3
source

First of all, XPath is not going to do any sorting except as part of the XSLT transformation. Secondly, XPath 1.0, used in .NET, does not have specific date support.

The easiest way is to load the XML into an XDocument and use such code (it is assumed that a number of "Message" nodes are under the root of the top level node in the document): -

 Func<XElement, DateTime> fn = e => DateTime.ParseExact(e.Element("date").Value, "dd.MM.yyyy", CultureInfo.InvariantCulture); var messages = doc.Root.Elements("Message").OrderBy(fn); foreach (var elem in messages) { Console.WriteLine(fn(elem)); } 

Alternatively, if you have a reason to stick with XmlDocument, this slightly ugly code will work: -

 Func<XmlElement, DateTime> fn = e => DateTime.ParseExact(e.SelectSingleNode("date").InnerText, "dd.MM.yyyy", CultureInfo.InvariantCulture); var messages = doc.DocumentElement.SelectNodes("Message") .Cast<XmlElement>().OrderBy(fn); foreach (var elem in messages) { Console.WriteLine(fn(elem)); } 
0
source

If you have control over the XML format, I would recommend changing the date format to ISO 8601. Thus, dates can be sorted like regular strings. For example, 02/05/2010 will be 2010-05-02. In addition, ISO 8601 is less controversial (month to day or day to month?)

0
source

Source: https://habr.com/ru/post/1300521/


All Articles