0

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.

3 Answers 3

5

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.)

Sign up to request clarification or add additional context in comments.

3 Comments

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

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

1 Comment

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)
0

what if the html is a invaild format xml?

it looks like we can not use xslt?

Any feedbacks?

1 Comment

google "beautifulsoup", "htmltidy", and the "html agility pack"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.