Tuesday, December 16, 2008

XML manupilation(1) --xsl(xslt) transformation

The goal of this series is to build a WCF service using Linq to XML to convert xml file to pipeline delimited text file.

If we have xls/xslt sheet, it is very easy for us to do xml converting.

For example we have a xml file:(from http://www.w3schools.com/xml/xml_xsl.asp). And a xsl sheet,simple.xsl.

It is very easy for us to convert or display using our style sheet. For example, we can add < ? xml-stylesheet type="text/xsl" href="simple.xsl" ? > for displaying.

Also using C# for converting is easy, here is a sample code:

using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Xml.Xsl;


public class ConvertXml2Txt {
private static void Main() {
XslTransform transform = new XslTransform();
transform.Load("our.xslt");
transform.Transform("test.xml", "test.txt", null);
}
}

So if we have xsl sheet, it is very easy for us to convert. But the hard thing is that most of the time we don't have xsl sheet, and to write the xsl sheet is very headache.

So in the later series, I am going to use LINQ to XML for exploration.

No comments: