1

I have an XML node structure in below format

<comments>
  <comment lastupdated='12/08/2014:08:08:08' moderate='false'>
     <id>user1</id>
     <message>some message</message>
     <replies>
         <comment moderate='false'>
            <id>user2</id>
            <message>some other message</message>
         </comment>
     </replies>
   </comment>
  <comment lastupdated='13/08/2014:12:08:40' moderate='false'>
     <id>user3</id>
     <message>some message</message>
     <replies>
         <comment moderate='false'>
            <id>user1</id>
            <message>some other message</message>
         </comment>
     </replies>
   </comment>
 </comments>

I found this XML to LINQ solution XMLdocument Sort want to know if this can be also achieved through XPath so that I don't need to load a new assembly. Also I am little confused with attributes and child nodes? in terms of performance which one is better? fetch by attribute value or child node?

Update 1

The solution suggested by @Timothy in this link seems promising

var sortedItems = myDoc.GetElementsByTagName("item").OfType<XmlElement>()
.OrderBy(item => DateTime.ParseExact(item.GetAttribute("sTime"), "MM/dd/yyyy h:mm:ss tt", null));
Community
  • 1
  • 1
Naga
  • 2,368
  • 3
  • 23
  • 33

1 Answers1

0

Do you mean XDocument.XPathSelectElements method? see this question: XPath and XPathSelectElement

Be aware that you can not select attributes directly. I'm not sure how performant this method is, you can always just iterate through the tree yourself though.

Community
  • 1
  • 1
Efrain
  • 3,248
  • 4
  • 34
  • 61