c# - How do you sort an XDocument parent node based its child? -


in c# trying sort xdocument using orderbydesending. goal read 1 of child nodes contains date\time stamp , reorder parent nodes.

i load xml saved file this:

xdocument doc = new xdocument(); doc= xdocument.load(filename); 

here example of xml

<ks>   <team>        <teamname>knights</teamname>     <teamcolor>blue</teamcolor>     <lastaccessed>5/9/2013 2:34:22 pm</lastaccessed>   </team>   <team>     <teamname>rangers</teamname>     <teamcolor>red</teamcolor>     <lastaccessed>5/9/2013 3:49:06 pm</lastaccessed>   </team>   <team>     <teamname>eagles</teamname>     <teamcolor>green</teamcolor>         <lastaccessed>5/9/2013 3:50:18 pm</lastaccessed>   </team> </ks> 

i reorder descending based on child element

i have tried following without luck

var results = doc.root.descendants("team").orderbydescending(p => p.element("lastaccessed"));  xdocument node = new xdocument(doc.descendants("ks").orderbydescending(x => x.element("team").element("lastaccessed").value.trim())); 

any suggestions how 1 sort xml parent nodes based on value stored in child node?

you should have gotten error said:

at least 1 object must implement icomparable.

please include these errors in questions.

you need provide child of team element supports icomparable interface. need value rather xelement .elements gives you.

change results line below , work.

var results = doc.descendants("team").orderbydescending(p => datetime.parse(p.element("lastaccessed").value)); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -