Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Using LINQ to XML Instead of XSLT for Transformations

Using LINQ to XML Instead of XSLT for Transformations

Transforming XML from one format to another is a common task for many developers. To do this, most of them leave the confines of their general purpose language and make calls to an XSLT library. But what if they didn't have to?

With LINQ to XML, it now becomes much easier to manipulate XML using C# and VB. Eric White describes how one can perform XSLT style transformations using C# 3.0.

The key to Eric's method is the ability to annotate XML nodes with additional information. Instead of altering the tree piece-meal, each pending change is stored as annotation against XElement it replaces. Eric writes

One advantage to taking this approach - as you formulate queries, you are always writing queries on the unmodified source tree. You need not worry about how modifications to the tree affect the queries that you are writing.

Once all of the pending changes have been generated, they are applied at one time. This is done via the XForm function, which creates a copy of the tree, making replacements where appropriate.

You can learn more about this technique and get a copy of the XForm function from Eric White's blog. 

Rate this Article