I would like to share some useful functions for you to use. This is part of an application that I will eventually share when I get it completed. In my last post, I showed how to create a FlowDocument from HTML, well I put that code into a function. The other function, takes a URL and takes the HTML and returns it as a string.
public static string ConvertUrlToHtmlText(string url) {
try {
HttpWebRequest request = HttpWebRequest.Create(new Uri(url)) as HttpWebRequest; HttpWebResponse response = request.GetResponse() as HttpWebResponse; StreamReader reader = new StreamReader(response.GetResponseStream()); string convertedText = reader.ReadToEnd(); return convertedText; } catch (WebException) { return null; } catch (Exception) { return null; }
}
public static FlowDocument ConvertTextToFlowDocument(string text) {
try { string xaml = HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(text, true); return XamlReader.Load(new XmlTextReader(new StringReader(xaml))) as FlowDocument; } catch (Exception) { return null; }
try { string xaml = HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(text, true); return XamlReader.Load(new XmlTextReader(new StringReader(xaml))) as FlowDocument; } catch (Exception) { return null;
The full code can be found at http://waddleton.net/Documents/HtmlConversion.zip