views:

356

answers:

1

Hello :)

I would like to sort my xml for which the inffered schema is placed below.

I would like to sort rows based on one column value (alphabetical)

Is it possible to do that in Linq to Xml? Or XSLT is my only option?

Thanks

Kamil

Ok, I removed schema and provide part of file

    <Matrix>
    ...
    <Rows>
      <Row>
        <Visible>1</Visible>
        <Columns>
          <Column>
            <ID>col_f</ID>
            <Value>
            </Value>
          </Column>
          <Column>
            <ID>col_0</ID>
            <Value>r00329</Value>
          </Column>
          <Column>
            <ID>col_1</ID>
            <Value>Gerbera "Ambiance" rosa-creme</Value>
          </Column>
          <Column>
            <ID>col_2</ID>
            <Value>
            </Value>
          </Column>
          <Column>
            <ID>col_dost</ID>
            <Value>Bl... Holland</Value>
          </Column>
          <Column>
            <ID>col_3</ID>
            <Value>0,000</Value>
          </Column>
          <Column>
            <ID>col_5</ID>
            <Value>0,000</Value>
            ...
+1  A: 

Sorted rows can be obtained as follows:

// assume rows is a reference to the <Rows> node
var query = from row in rows.Elements( "Row" )
            let sortValue = (
                from c in row.Element("Columns").Elements("Column")
                where c.Element("ID").Value == "col_1"
                select c.Element("Value").Value
            ).FirstOrDefault()
            orderby sortValue
            select row;

This will give you a collection of "Row" elements sorted by the "col_1" column's "Value".

Adjust as needed.

Timothy Walters