This project is read-only.
Project Description
A simple project to facilitate converting badly formed html into WinRT-compatible xaml markup.


It turns out that converting html into Windows 8 xaml is more complex than I had imagined initially. A commendable start has been made by Vincent H at RichTextBlock.Html2Xaml. Since his solution depends on an xslt conversion, however, it tends not to be useful for badly-formed html. A lot of what I was working with had really poor lists and tables and a simple xslt translation just became unworkable.

In the end, I ended up using his framework to get the html bound to the RichTextBlock element, but dropped down to my own library for converting the html. Since no other solution I've found handles the kind of scenario I'm embroiled in, I thought I'd make my library accessible to others. This library makes use of the marvelous Html Agility Pack to parse the html and a set of statically configured tag definitions to parse html into Xaml.

Customization at this point (besides downloading the code and making your own adjustments) is limited to being able to add attributes to the Xaml elements based on the html tag used. This will allow desired formatting, but is pretty limiting all-told. If demand exists, we can look at building in more functionality.

I've never done a NuGet package, but that's probably another good idea to get around to. For now, here's what we have.

Using the library

Download the source or binary and add the reference in your project. When it comes time to convert into Xaml, call one of the two overloads on the Html2XamlConverter class:

Html2XamlConverter.Convert2Xaml(string HtmlString)
Html2XamlConverter.Convert2Xaml(string HtmlString, Dictionary<string, Dictionary<string, string>> TagAttributers)

The second is the one that allows you to add attributes to the Xaml elements emitted. The first key is the html tag, the second is the attribute/value key pair to add. Calling this should go something like:

string XamlString = Html2XamlConverter.Convert2Xaml(Text, new Dictionary<string, Dictionary<string, string>> { 
	{ "p", new Dictionary<string, string> { { "Margin", "0,10,0,0" } } },
	{"table", new Dictionary<string, string> { { "Foreground", "#FF663C00" } } }

Last edited Dec 31, 2012 at 7:17 PM by Proffitt, version 2