vb.net - LINQ Order By Query for XML -


Hello I have the following problem:

I am trying to do the following on an XML file: Fix all STRUCTs according to the ID attribute of STRING but return a list of content objects XML can contain more than one element.

  & lt; Object & gt; & Lt; Content & gt; & Lt; Struct & gt; & Lt; STRING id = "2" & gt; String & lt; / STRING & gt; & Lt; / Struct & gt; & Lt; Struct & gt; & Lt; STRING id = "1" & gt; String 1 & lt; / STRING & gt; & Lt; / Struct & gt; & Lt; / Content & gt; & Lt; / Object & gt; & Lt; Object & gt; & Lt; Content & gt; & Lt; Struct & gt; & Lt; STRING id = "345" & gt; String & lt; / STRING & gt; & Lt; / Struct & gt; & Lt; Struct & gt; & Lt; STRING id = "333" & gt; String & lt; / STRING & gt; & Lt; / Struct & gt; & Lt; / Content & gt; & Lt; / Object & gt;  

I am using the following LINQ query, but the stars are not sorted:

  dim content = origin from nm to origXML. Descendants ("CONTENT") ID = nm. Element ("Structus"). Element ("STRING"). Order the attribute ("id") and select the nm for each XML string in ascending order.  

desired output

  & lt; Object & gt; & Lt; Content & gt; & Lt; Struct & gt; & Lt; STRING id = "1" & gt; String & lt; / STRING & gt; & Lt; / Struct & gt; & Lt; Struct & gt; & Lt; STRING id = "2" & gt; String 1 & lt; / STRING & gt; & Lt; / Struct & gt; & Lt; / Content & gt; & Lt; / Object & gt; & Lt; Object & gt; & Lt; Content & gt; & Lt; Struct & gt; & Lt; STRING id = "333" & gt; String & lt; / STRING & gt; & Lt; / Struct & gt; & Lt; Struct & gt; & Lt; STRING id = "345" & gt; String & lt; / STRING & gt; & Lt; / Struct & gt; & Lt; / Content & gt; & Lt; / Object & gt;  

I know that there are other ways to do this, but I want to know whether it is possible using LINQ?

thanks

Rob

I do not think one Single linq query can give you the desired output to properly create a new XML document, whose basic structure is similar to the original structure, you need nested linear queries applied to the original XML document.

Assuming that the root node of your XML code & lt; OBJECTS & gt; , the following part of the code should give you the desired output:

  Appearance = New Excellence ("Objects", Obj in Injenet ML Descendants ("OBJECT") Select XElement ("Object", from content, obj.Descendants ("CONTENT") Select New XElement ("CONTENT", content from "Content" ("STRUCT") ID = Convert.ToInt32 (St. Element "STRING"). Attribute ("id"). Value) Select new selection from ascending order xe ("id", "id", id))))))  

Comments