Mithil
Mithil

Reputation: 3770

XML, LINQ parsing

I am modifying my XML using LINQ:

Dim feedXML As XDocument = XDocument.Parse(m_xmld.OuterXml.ToString())
Dim SortedFields = From field In feedXML.Descendants("fields")
Dim sFieldList = From field In SortedFields.Descendants("field") Order By
                 Integer.Parse(field.@position)

I am trying to sort my "fields" in ascending order. Now my problem is I want the sorted fields to replace my unsorted fields list in the XML so that I can use the sorted XML further.

How can I retrieve the XML after the sorting?

Upvotes: 1

Views: 698

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500525

There's no real concept of "the XML after the sorting". If you've only got field elements, it's relatively easy - but if you've got:

 <field position="2" />
 <non-field />
 <field position="1" />
 <non-field />
 <field position="0" />

then what should the result be afterwards?

Upvotes: 2

Gert Jan Kamstra
Gert Jan Kamstra

Reputation: 1

I used ReplaceNodes for this in the end:

x.ReplaceNodes(
    from el in x.Elements()
    orderby (int)el.Element("Index")
    select el                               
);

Upvotes: 0

Related Questions