c# - Sorting XDocument elements based on InnerXML -
i have xml document similar this:
<document> <post> <author>bill smith</author> <subject>test article</subject> <dates> <uploaded>some date</uploaded> <published>some date</published> </dates> <price> <provider>amazon</provider> <cost>1540</cost> </price> <price> <provider>wh smith</provider> <cost>2640</cost> </price> </post> <post> <author>bill smith</author> <subject>test article</subject> <dates> <uploaded>some date</uploaded> <published>some date</published> </dates> <price> <provider>amazon</provider> <cost>1540</cost> </price> <price> <provider>wh smith</provider> <cost>2640</cost> </price> </post> </document> i'm using xdocument w/ .net 4.5. know there other methods use sort this.
i have working ok pull each post , put post model. however, sort price elements , pick out lowest price (and provider) can insert ef database.
any appreciated, i'm totally stuck on start this.
you can give try:
xdocument doc = xdocument.load(@"data.xml"); var elements = doc.root .elements("post") .select(post => new { author = post.element("author").value, subject = post.element("subject").value, uploaded = convert.todatetime(post.element("dates").element("uploaded").value), published = convert.todatetime(post.element("dates").element("published").value), price = new { p = post .elements("price") .orderbydescending(price => convert.todecimal(price.element("cost").value)) .select(o => new { provider = o.element("provider").value, cost = convert.todecimal(o.element("cost").value) }) .first() } }); var p = elements.first();
Comments
Post a Comment