Encoder.cs

// Copyright � 2007 Jeffrey Bazinet, http://www.vwd-cms.com/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace VwdCms.Admin
{
    public class Encoder : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Button btnGo;
        protected System.Web.UI.WebControls.TextBox txtText;
        protected System.Web.UI.WebControls.RadioButtonList rblAction;
        protected System.Web.UI.WebControls.CheckBox chkReplaceLineFeeds;
        protected System.Web.UI.WebControls.CheckBox chkReplaceTabs;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.btnGo.Command += new CommandEventHandler(btnGo_Command);
        }

        void btnGo_Command(object sender, CommandEventArgs e)
        {
            string text = this.txtText.Text;

            switch (this.rblAction.SelectedValue)
            {
                case "htmlencode":
                    text = HttpUtility.HtmlEncode(text);
                    break;
                case "htmldecode":
                    text = HttpUtility.HtmlDecode(text);
                    break;
                case "urldecode":
                    text = HttpUtility.UrlDecode(text);
                    break;
                case "urlencode":
                    text = HttpUtility.UrlEncode(text);
                    break;
                case "urlpathencode":
                    text = HttpUtility.UrlPathEncode(text);
                    break;
                case "xmlencode":
                    text = XmlEncode(text);
                    break;
                case "xmldecode":
                    text = XmlDecode(text);
                    break;
            }

            if (this.chkReplaceLineFeeds.Checked)
            {
                text = text.Replace("\r", string.Empty);
                text = text.Replace("\n", "<br />\r\n");
            }

            if (this.chkReplaceTabs.Checked)
            {
                text = text.Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
            }

            this.txtText.Text = text;
        }

        private string XmlEncode(string code)
        {
            XmlDocument doc = new XmlDocument();
            doc.PreserveWhitespace = true;
            XmlNode node = doc.CreateNode(XmlNodeType.Element, "xmlencoder", null);
            node.InnerText = code;
            return node.InnerXml;
        }
        private string XmlDecode(string code)
        {
            XmlDocument doc = new XmlDocument();
            doc.PreserveWhitespace = true;
            XmlNode node = doc.CreateNode(XmlNodeType.Element, "xmldecoder", null);
            node.InnerXml = code;
            return node.InnerText;
        }
    }
}