You are not Logged in
Would you like to Login or Register

Today is: Saturday, 19 July, 2008
Check this months hot topics

Sorting an XML document in C# using XSL

I needed to sort some XML in C# before itterating through the document. In the end I used the XslCompiledTransform to apply an XSL stylesheet to the XML document. Works very quickly. For reference, this is how I did it.

The code

The following uses an XslCompiledTransform object to apply an XSL transformation. The output is written to a StringBuilder and reloaded into the successes XML document.

 // sort the successes based on priority
 System.Text.StringBuilder sortedXml = new System.Text.StringBuilder();
 XmlWriterSettings settings = new XmlWriterSettings();
 settings.Indent = false;
 XmlWriter writer = XmlWriter.Create(sortedXml, settings);

 XslCompiledTransform sortXsl = new XslCompiledTransform();
 sortXsl.Load(Server.MapPath(@"~/SuccessSort.xsl"));
 sortXsl.Transform(successes, writer);
 writer.Close();
 
 successes.LoadXml(sortedXml.ToString());

The XSL in my SuccessSort.xsl document

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:template match="successes">
    <successes>
      <xsl:apply-templates select="success">
        <xsl:sort select="priority" data-type="number" order="descending"/>
      </xsl:apply-templates>
    </successes>
  </xsl:template>
  <xsl:template match="success">
    <xsl:copy-of select="." />
  </xsl:template>
</xsl:stylesheet>

The XML inside my successes XmlDocument

<successes>
  <success>
    <priority>5</priority>
    <!-- stuff -->
  </success>
  <success>
    <priority>2</priority>
    <!-- stuff -->
  </success>
  <success>
    <priority>3</priority>
    <!-- stuff -->
  </success>
</successes>
kick it on DotNetKicks.com del.icio.us digg Mister Wong YahooMyWeb Reddit Furl Spurl blogmarks
Paul Hayman Skype
Author : Paul Hayman
Published : Wednesday, 20 June, 2007

Paul is the COO of kwiboo ltd consultant and has more than a decade of IT consultancy experience. He has consulted for a number of blue chip companies and has been exposed to the folowing sectors: Utilities, Telecommunications, Insurance, Media, Investment Banking, Leisure, Legal, CRM, Pharmaceuticals, Interactive Gaming, Mobile Communications, Online Services. Paul is the COO and co-founder of kwiboo (http://www.kwiboo.com/) and is also the creator of GeekZilla.

Comments

ricardo said:

Can you give me some hits about using your code to sort this xml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<campos>

  <ComboBox indice="4">
    <nome>nome</nome>
    <conn>Server</conn>
    <tipo>sqlserver</tipo>
    <sql>SELECT</sql>
    <descricao>Nome do Utilizador</descricao>
  </ComboBox>
  <Data indice="1">
    <nome>dataDigi</nome>
    <descricao>Data Digitalização</descricao>
  </Data>
  <config>
    <repositorio>c:\rep\</repositorio>
  </config>
  <ComboBox2 indice="3">
    <nome>TipoEntidade</nome>
    <descricao>Tipo Entidade</descricao>
    <campos>
      <campo>Cliente</campo>
      <campo>Fornecedor</campo>
    </campos>
  </ComboBox2>
  <ComboBox3 indice="2">
    <nome>combo1</nome>
    <descricao>descricao1</descricao>
    <nome2>combo2</nome2>
    <descricao2>descricao2</descricao2>
    <tipo>combobox</tipo>
    <campos>
      <campo>
        <id>clientes</id>
        <conn>Server</conn>
        <tipoBD>sqlserver</tipoBD>
        <sql>SELECT</sql>
      </campo>
      <campo>
        <id>fornecedores</id>
        <conn>Provider</conn>
        <tipoBD>access</tipoBD>
        <sql>SELECT</sql>
      </campo>
    </campos>
  </ComboBox3>
  <ComboBox indice="0">
    <nome>formandos</nome>
    <conn>Server</conn>
    <tipo>sqlserver</tipo>
    <sql>SELECT</sql>
    <descricao>Formandos</descricao>
  </ComboBox>

</campos>

Thanks!

August 10, 2007 - 12:20 PM

phayman said:

How would you like to sort it?

August 10, 2007 - 1:09 PM

Add Comment

Enter your comment below and it will be submitted for moderation.

Your Name

Add Tag

Please enter tags for this article, seperated by semi-colon ;

View Tag's by : # articles | # views