tags:

views:

112

answers:

2
+1  Q: 

XML sorting

I have the following XML

<fields>
  <field position="4" tablename="Students" headername="First Name" fieldreference="FirstName" orderbydirection="ASC" />
  <field position="2" tablename="Students" headername="Last Name" fieldreference="LastName" orderbydirection="ASC" />
  <field position="3" tablename="Students" headername="Race" fieldreference="Race" orderbydirection="ASC" />
  <field position="1" tablename="Students" headername="Sex" fieldreference="Sex" orderbydirection="ASC" />
  <field position="5" tablename="Students" headername="State" fieldreference="State" orderbydirection="ASC" />
</fields>

I want to sort it based on the "position"

How can it be done?

Thanks in advance!

+1  A: 

See sorting with XSLT http://www.xml.com/pub/a/2002/07/03/transform.html

Pierre
+1  A: 

This XSLT 1.0 transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:strip-space elements="*"/>

    <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="fields">
      <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates>
          <xsl:sort select="@position" data-type="number"/>
        </xsl:apply-templates>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<fields>
    <field position="4" tablename="Students" headername="First Name" fieldreference="FirstName" orderbydirection="ASC" />
    <field position="2" tablename="Students" headername="Last Name" fieldreference="LastName" orderbydirection="ASC" />
    <field position="3" tablename="Students" headername="Race" fieldreference="Race" orderbydirection="ASC" />
    <field position="1" tablename="Students" headername="Sex" fieldreference="Sex" orderbydirection="ASC" />
    <field position="5" tablename="Students" headername="State" fieldreference="State" orderbydirection="ASC" />
</fields>

produces the wanted result:

<fields>
  <field position="1" tablename="Students" headername="Sex" fieldreference="Sex" orderbydirection="ASC" />
  <field position="2" tablename="Students" headername="Last Name" fieldreference="LastName" orderbydirection="ASC" />
  <field position="3" tablename="Students" headername="Race" fieldreference="Race" orderbydirection="ASC" />
  <field position="4" tablename="Students" headername="First Name" fieldreference="FirstName" orderbydirection="ASC" />
  <field position="5" tablename="Students" headername="State" fieldreference="State" orderbydirection="ASC" />
</fields>
Dimitre Novatchev
I was able to do this using LINQ in 2 lines of code.Thakns everyone
Mithil Deshmukh
What was the Link code?
Dim sFieldList = From field In SortedFields.Descendants("field") Order By Integer.Parse(field.@position)
Mithil Deshmukh
Actually here is the full codeDim feedXML As XDocument = XDocument.Parse(sXMLDefinition.ToString())Dim SortedFields = From field In feedXML.Descendants("fields")Dim sFieldList = From field In SortedFields.Descendants("field") Order By Integer.Parse(field.@position)
Mithil Deshmukh