www.RoomSay.com

HTML5 and CSS3 enabled 3D web experience at roomsay.com:

RoomSay.com 3D Web Experience RoomSay.com 3D Web Experience DIY

Vectorlight News

  • 3D CSS used to Create Website
    Feb 08, 2014

    With the CSS 3D effects being implemented in modern browsers RoomSay.com takes this new technology and enables you to map your traditional 2D content onto the walls of a 3D room.

  • Chat App Converted to HTML and JQuery
    Sep 08, 2011

    Converted from Silverlight to HTML and Javascript/JQuery is the Vectorlight Chat App. Login using your Vectorlight password to chat using your username and avatar.

  • HTML5 iPhone,Android Big Guns Tower Defense
    Jul 02, 2011

    Big Guns has made the leap from Windows Phone 7 (XNA) to HTML5 so you can now play it on your iPhone, Android and other HTML5 compatible devices.

  • HTML5 Games - Word Poppers and Batty
    Jun 04, 2011

    As the take-up of HTML5 quickens (74% of users currently have a browser capable of HTML5 Canvas) we present two more games for both your browser and mobile.

  • Big Guns Tower Defense on Windows Phone 7
    May 06, 2011

    Coming soon to Windows Phone 7 is an XNA port of the popular Vectorlight tower defense game Super Tower Defense. Whilst retaining many of the graphical and gameplay features of the original Silverlight game.

  • Home Page News

Rendering HTML in Silverlight

Whilst not an obviously useful requirement, converting Rich Text to and from HTML would be very useful for applications such as Forums, web CMS's etc, the problem is getting RichText into our Silverlight application.

Using the Liquid RichTextBox we can enter RichText into a Silverlight application and then retrieve the input as XML and convert it (roughly) to HTML.

Whilst it seems like a simple task there are lots of things to take into account when trying to convert such as class/style names, bulleted lists etc.  The methods presented here in this static class were quickly nocked up in a few hours and can still be improved.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Xml;

namespace Liquid.Components
{
    public class Html
    {
        #region Public Methods

        public static string[] ConvertRichTextToHTML(string richTextXML)
        {
            Dictionary<string, RichTextBoxStyle> styleList = new Dictionary<string, RichTextBoxStyle>();
            RichTextBoxStyle style;
            XmlReader reader;
            string styleID;
            string temp;
            string tag;
            string optional;
            StringBuilder html = new StringBuilder();
            StringBuilder styles = new StringBuilder();
            

            reader = XmlReader.Create(new StringReader(richTextXML));
            reader.Read();

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    temp = reader.Name.ToLower();

                    if (temp == RichTextBox.StyleElement)
                    {
                        style = new RichTextBoxStyle(reader);
                        styleList.Add(style.ID, style);

                        if (IsStyleAHeading(style.ID.ToLower()))
                        {
                            tag = "";
                        }
                        else
                        {
                            tag = ".";
                        }

                        styles.Append(tag + style.ID + " {");

                        styles.Append("font-family:" + style.Family + "; ");
                        styles.Append("font-size:" + style.Size.ToString() + "px; ");
                        styles.Append("text-align:" + style.Alignment.ToString() + "; ");
                        styles.Append("color:#" + ((SolidColorBrush)style.Foreground).Color.ToString().Substring(3) + "; ");

                        if (style.Weight == FontWeights.Bold)
                        {
                            styles.Append("font-weight:bold; ");
                        }
                        if (style.Decorations == TextDecorations.Underline)
                        {
                            styles.Append("text-decoration:underline; ");
                        }
                        if (style.Style == FontStyles.Italic)
                        {
                            styles.Append("font-style:italic; ");
                        }
                        if (style.Special == RichTextSpecialFormatting.Subscript)
                        {
                        }
                        else if (style.Special == RichTextSpecialFormatting.Superscript)
                        {
                        }

                        styles.Append(" }\r\n");
                    }
                    else if (temp == RichTextBox.TextElement)
                    {
                        styleID = reader.GetAttribute("Style");
                        reader.Read();

                        optional = "";
                        if(IsStyleAHeading(styleID.ToLower()))
                        {
                            tag = styleID.ToLower();
                        }
                        else if (styleList[styleID].Link.Length > 0)
                        {
                            tag = "a";
                            optional = " href='" + styleList[styleID].Link + "' class='" + styleID + "'";
                        }
                        else
                        {
                            tag = "span";
                            optional = " class='" + styleID + "'";
                        }

                        if (reader.NodeType == XmlNodeType.CDATA)
                        {
                            html.Append("<" + tag + optional + ">" + reader.Value + "</" + tag + ">");
                        }
                    }
                    else if (temp == RichTextBox.NewlineElement)
                    {
                        reader.Read();
                        html.Append("<br />");
                    }
                    else if (temp == RichTextBox.XamlElement)
                    {
                        continue;
                    }
                }
            }

            reader.Close();

            return new string[] { styles.ToString(), html.ToString() };
        }

        public static string ConvertRichTextToHTMLDocument(string richTextXML)
        {
            string[] temp = ConvertRichTextToHTML(richTextXML);

            return "<html><head><style>" + temp[0] + "</style></head><body>" + temp[1] + "</body></html>";
        }

        #endregion

        #region Private Methods

        private static bool IsStyleAHeading(string styleID)
        {
            return (styleID == "h1" || styleID == "h2" || styleID == "h3" || styleID == "h4");
        }

        #endregion
    }
}


The conversion using the above class does a very basic translation to HTML, and if you run the example you will see plenty of room for improvement.  In terms of converting from HTML to RichText, well, that article is on the way...

Your Comments

kanchirk posted

A Very good write up and excellent example.



Thanks for sharing the code.



It has been Quite helpful for me.



Regards



Raghuraman


 

Post your Comments

Rate this: 

1 Star 2 Star 3 Star 4 Star 5 Star
18 Ratings / 3.7 Average

Tweets

Silverlight Controls

  • Rich TextBox

    Create and edit rich content with this slick and expandable Rich TextBox...

  • TreeView

    This easy to use TreeView comes with drag and drop, sorting, searching and much more...

  • Context Menu

    You too can have cool popup context menus in your Silverlight applications...

  • Resizable Dialog

    Draggable and resizable popup dialogs are what serious Silverlight developers need...

  • Spell Checker

    Real-time spell checking in Silverlight? We did it first here...