vb.net - LINQ Order By Query for XML -
hi have next problem:
i trying next on xml file: sort structs according id attribute of string. homecoming list of content objects. xml can contain more 1 element.
<object> <content> <struct> <string id="2">string</string> </struct> <struct> <string id="1">string1</string> </struct> </content> </object> <object> <content> <struct> <string id="345">string</string> </struct> <struct> <string id="333">string</string> </struct> </content> </object> i using next linq query, strings not beingness sorted:
dim contents = nm in origxml.descendants("content") allow id = nm.element("struct").element("string").attribute("id") order id ascending select nm each xmlstring in contents.... desired output
<object> <content> <struct> <string id="1">string</string> </struct> <struct> <string id="2">string1</string> </struct> </content> </object> <object> <content> <struct> <string id="333">string</string> </struct> <struct> <string id="345">string</string> </struct> </content> </object> i know there must other ways this, want know if it's possible using linq?
thanks
rob
i don't think single linq query can give desired output. in order create new xml document, has same construction original one, need multiple nested linq queries applied original xml document.
assuming root node of xml <objects>, next piece of code should give desired output:
dim xelement xelement = new xelement("objects", obj in origxml.descendants("object") select new xelement("object", content in obj.descendants("content") select new xelement("content", st in content.descendants("struct") allow id = convert.toint32(st.element("string") .attribute("id").value) order id ascending select new xelement("struct", new xelement("string", st.element("string").value, new xattribute("id", id))) ) ) ) xml vb.net linq
No comments:
Post a Comment