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.Transform(successes, writer);

The XSL in my SuccessSort.xsl document

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

<xsl:stylesheet xmlns:xsl=""
  <xsl:template match="successes">
      <xsl:apply-templates select="success">
        <xsl:sort select="priority" data-type="number" order="descending"/>
  <xsl:template match="success">
    <xsl:copy-of select="." />

The XML inside my successes XmlDocument

    <!-- stuff -->
    <!-- stuff -->
    <!-- stuff -->
Author Paul Hayman

Paul is the COO of kwiboo ltd and has more than 20 years 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 ( and is also the creator of GeekZilla.


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"?>


  <ComboBox indice="4">
    <descricao>Nome do Utilizador</descricao>
  <Data indice="1">
    <descricao>Data DigitalizaĆ§Ć£o</descricao>
  <ComboBox2 indice="3">
    <descricao>Tipo Entidade</descricao>
  <ComboBox3 indice="2">
  <ComboBox indice="0">



10/Aug/2007 12:20 PM

phayman said:

How would you like to sort it?

10/Aug/2007 13:09 PM

Francisco said:

Thank you very much, your code really helped me!

31/Oct/2008 23:00 PM

ghulam Haider said:

What is successes? C# gives error that is is not defined. Please help me. I need you code urgently.

28/Nov/2008 09:23 AM

phayman said:

successes is the root node of my XML document


28/Nov/2008 11:19 AM

Ghulam Haider said:

Thank you very much for you shortest code. I could not run your code till my PM help me and run your code after some amendments. Sorry I am new to XML. Again thanks for your response. This is our xml file <?xml version="1.0" encoding="utf-8" ?>


  <video name="name1" url="url1" id="1">


// 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);

        XPathDocument myXPathDocument = new XPathDocument(Server.MapPath(@"~/XMLFile.xml"));

        XslCompiledTransform sortXsl = new XslCompiledTransform();
        sortXsl.Transform(myXPathDocument, writer);

        XmlDocument xmlDoc = new XmlDocument();


        Label1.Text = sortedXml.ToString();
29/Nov/2008 07:49 AM

eMan said:

sortXsl.Transform(successes, writer);

What is 'successes' here. I mean, what is its datatype. Is it a XmlDocument??

05/Feb/2009 07:03 AM

eMan said:

hai Paul

 sortXsl.Transform(successes, writer);

Here what is the datatype of "successes". Is it a SYstem.Xml.XmlDocument


10/Feb/2009 05:07 AM

ibrucken said:

Hello Paul

Very usefull sample for me.

Could you tell me how to do the sort by attribute instead of node?

Thank you.

25/Feb/2009 20:43 PM

phayman said:


<xsl:sort select="@yourattribute" data-type="number" order="descending"/>

23/Mar/2009 10:33 AM

Paco Marquez said:

Great example man, this help me a lot!


10/Aug/2010 23:12 PM

Kenny DellaValle said:

This post was extremely helpful, thanks for the post!

15/Feb/2011 03:05 AM

Add Comment


Your comment has been received and will be shown once it passes moderation.