tags:

views:

128

answers:

3

I have this kinda interesting requirement.

Typically you use XSLT to transform an XML document. The transformed HTML is viewable in a web browser, this works just great. I am also guessing the browser handles the transformation in memory, because if you view the page source of an xml document with XSLT, you don't see html, only xml.

What I would like to do is the following.

using c#

  1. grab an xml file from the fileSystem.... Load it into some framework object
  2. attach an XSLT stylesheet
  3. output the rendered HTML back to an html file on the file system.

Is this possible.

I don't expect a full answer on the entire solution. Just a push in the right direction would be great :) thanks in advance.

+5  A: 

You can use System.Xml.Xsl to do XSLT in C#.

There's an article here: XML transformation using Xslt in C# that explains how - here's the core of it:

XPathDocument myXPathDoc = new XPathDocument(<xml file path>);
XslTransform myXslTrans = new XslTransform();
myXslTrans.Load(<xsl file path>);
XmlTextWriter myWriter = new XmlTextWriter("result.html", null);
myXslTrans.Transform(myXPathDoc, null, myWriter);

(Edit: Note to @John: that code illustrates the basic idea. It doesn't pretend to be production quality.)

RichieHindle
I marked your answer as correct, cause you posted a better article.
JL
Please add a using block for the text writer, and maybe use XmlWriter.Create?
John Saunders
@Richie: example code gets copied and pasted, and people whine and say, "but Richie Hindle said it was ok". Hence, -1.
John Saunders
A: 

So, I found the answer, and pretty quickly... Its all explained here... http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63

JL
If you found your answer and quickly, why did you post the question on here first without trying it in a search provider (such as google for instance)
ThePower
A: 

what if the html is a invaild format xml?

it looks like we can not use xslt?

Any feedbacks?

ariso
google "beautifulsoup", "htmltidy", and the "html agility pack"
annakata
THANK YOU , ANNAKATA
ariso