Question

Let's say I'm crazy and I want to implement an basic HTML rendering engine with c#. I would like to use WPF Controls to display the HTML Layout.

While there is nothing to gain with such an idea, I still want to try it out! So what libraries, projects and documentation would help me get this done?

Was it helpful?

Solution

Since WPF XAML's capabilities are a superset of HTML's capabilities, pretty much all you will need to do is map the one to the other.

If your input is XHTML you can simply use XSLT or LINQ-to-XML to get the data.

If you use XSLT you will end up with a XAML that you parse with XamlReader.Parse() to get a UIElement you can add to your Window or other WPF container. This is probably more work than using LINQ-to-XML because XSLT is relatively limited.

If you use LINQ-to-XML you can choose to produce XAML and then parse it as in the XSLT case, or you can just create the object tree directly. If you create XAML using LINQ-to-XML, I would write the actual translation code in VB.NET instead of C# which is my normal preferred language. VB.NET's "XML Literal" syntax makes that part of the job easier.

If your input is arbitrary HTML and not XHTML, I would use a third party library to convert it to XHTML and work from there. Handling all of HTML's parsing strangeness is a lot of work and probably mostly unrelated to your actual goal.

If you just want to display HTML without turning it into WPF objects, you can use the Frame control.

OTHER TIPS

Rendering html will be easy just convert to xaml file and render should be quite fast. You will get a HW accelerated browser with beautiful zoom and fonts.

But how do you handle css - you will need to create styles ( Yuck) ? DHTML , HTML5 and Java script .. So many pages now require these thinsg you cant even view it.

The easy way for Java script is to use the parser everyone uses , there may be a better way ( and Java script to CIL compilers ?)

html parsers, with a typed mapping to xaml alternatives, interesting... perhaps you can use antlr for the parser lexer http://www.antlr.org/, more specifically heres an interesting codeproject, project http://www.codeproject.com/KB/cs/TagBasedHtmlParser.aspx, you could perhaps try transform your html into xaml, which would be quite interesting :)

I think this project is exactly what you aim to achieve:
https://htmlrenderer.codeplex.com/
Maybe you could work on it and add HTML 5 and CSS 3 support!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top