RTF to XML : convert RTF (rich text) to and from XML

Shows how to use an RTF form and save it's content to an XML file and back again

Download rtf2xml.zip

Synopsis:

rtf2xml.cs


rtf2xml.cs

Synopsis
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;

namespace csharp.rtf2xml
{
	public class rtf2xml
	{
		public rtf2xml()
		{
			//
			// TODO: Add constructor logic here
			//
		}
	}

  /// <summary>
  /// Summary description for rtf2xml.
  /// </summary>
  public class rtf2xmlForm : System.Windows.Forms.Form
  {
    private XmlDocument mXml = new XmlDocument();

    private System.Windows.Forms.RichTextBox mRtfBox;
    private System.Windows.Forms.Button mBtnSave;
    private System.Windows.Forms.Button mBtnLoad;
    private System.Windows.Forms.Button mBtnClear;

    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public rtf2xmlForm()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.mRtfBox = new System.Windows.Forms.RichTextBox();
      this.mBtnSave = new System.Windows.Forms.Button();
      this.mBtnLoad = new System.Windows.Forms.Button();
      this.mBtnClear = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // mRtfBox
      // 
      this.mRtfBox.Location = new System.Drawing.Point(232, 80);
      this.mRtfBox.Name = "mRtfBox";
      this.mRtfBox.Size = new System.Drawing.Size(264, 96);
      this.mRtfBox.TabIndex = 0;
      this.mRtfBox.Text = "blah blah";
      // 
      // mBtnSave
      // 
      this.mBtnSave.Location = new System.Drawing.Point(40, 32);
      this.mBtnSave.Name = "mBtnSave";
      this.mBtnSave.TabIndex = 1;
      this.mBtnSave.Text = "Save";
      this.mBtnSave.Click += new System.EventHandler(this.mBtnSave_Click);
      // 
      // mBtnLoad
      // 
      this.mBtnLoad.Location = new System.Drawing.Point(40, 72);
      this.mBtnLoad.Name = "mBtnLoad";
      this.mBtnLoad.TabIndex = 2;
      this.mBtnLoad.Text = "Load";
      this.mBtnLoad.Click += new System.EventHandler(this.mBtnLoad_Click);
      // 
      // mBtnClear
      // 
      this.mBtnClear.Location = new System.Drawing.Point(40, 112);
      this.mBtnClear.Name = "mBtnClear";
      this.mBtnClear.TabIndex = 3;
      this.mBtnClear.Text = "Clear";
      this.mBtnClear.Click += new System.EventHandler(this.mBtnClear_Click);
      // 
      // rtf2xmlForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(688, 293);
      this.Controls.Add(this.mBtnClear);
      this.Controls.Add(this.mBtnLoad);
      this.Controls.Add(this.mBtnSave);
      this.Controls.Add(this.mRtfBox);
      this.Name = "rtf2xmlForm";
      this.Text = "rtf2xmlForm";
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new rtf2xmlForm());
    }

    private void mBtnSave_Click(object sender, System.EventArgs e)
    {
      mXml = new XmlDocument();
      XmlNode root = mXml.CreateElement("rtftest");
      mXml.AppendChild(root);
      XmlElement n = mXml.CreateElement("rtftext");

      //The Rtf property holds the contents of the edit box as rtf format
      n.InnerText = mRtfBox.Rtf;

      root.AppendChild(n);
      mXml.Save("x.xml");
    }

    private void mBtnLoad_Click(object sender, System.EventArgs e)
    {
      mXml = new XmlDocument();
      mXml.Load("x.xml");

      //find the rtftext node and ...
      XmlNode n =  mXml.DocumentElement.SelectSingleNode("rtftext");

      //load it into the edit box
      mRtfBox.Rtf = n.InnerText;
    }

    private void mBtnClear_Click(object sender, System.EventArgs e)
    {
      //clear it out
      mRtfBox.Rtf = null;
    }
  }
}






Contact me about content on this page using john_web-at-arrizza-dot-com
For Web Master or site problems contact: webadmin-at-arrizza-dot-com
Copyright John Arrizza (c) 2001-2010