Wednesday, March 10, 2004

XSL in ASP.NET the Easy way

Well Boys and Girls, I am about to get geek here. I've just spend an hour trying to figure out how to write a function in C# that will let me display my xml transformed with my XSL Stylesheet. That is nothing cool then seperating the data from the presentation layer! So how come it took an hour? Well, because I was thinking in Classic ASP mode. Yup we all do it from time to time. You see in Classic ASP if you were going to transform your xml with an xsl it looked something like this:
dim xmlDoc, xslDoc
Set xml = Server.CreateObject("MSXML2.DOMDocument.4.0")
xml.async = false

Set xsl = Server.CreateObject("MSXML2.DOMDocument.4.0")
xsl.async = false

Response.Write (xml.transformNode(xsl))

So of course this is how I was thinking that I was going to have to do it in .NET. I won't bore you with the details of everything I tried. Let's just say that after trying the StreamWriter and still not getting what I wanted I was frustrated. I was thinking this can't be harder in ASP.NET then it was in Classic ASP, that would be a step in the wrong direction. So I did one more google search. Ah-ha, there was what I was looking for. It was easy I knew it had to be. So here is how to do the same thing in ASP.NET...Ready

< asp:Xml ID="books" DocumentSource="xml/books.xml" TransformSource="xsl/output.xsl" Runat="server" />
Yup, that's all there is too it! I have to thank for doing such a great job of documenting the controls and giving great example code.

Happy coding :-)

No comments: