Click here to Skip to main content
15,885,914 members
Articles / Desktop Programming / Windows Forms

How to Harness the Power of XHTML and XForms in your .NET Applications

Rate me:
Please Sign up or sign in to vote.
4.15/5 (9 votes)
11 Feb 2009CDDL4 min read 98K   258   43   30
XForms is an important recommendation from the W3C that enables complex XML-handling applications to be defined in a simple, declarative syntax. This article demonstrates how you can leverage this power in your own applications.
Screenshot

Introduction

XForms is an important recommendation from the W3C that enables complex XML-handling applications to be defined in a simple, declarative syntax. This article demonstrates how you can harness this technology in your own C# applications. However, the same techniques may be easily applied to other .NET languages, or indeed any programming language that has a COM binding. The XHTML and XForms rendering power at the heart of it all will be provided by Ubiquity formsPlayer, a free open-source XForms processor.

Why XForms?

The advantages of XForms over other solutions have been argued far more eloquently elsewhere than I could manage, so I won't go into great detail here. Instead, those of you interested in reading around the subject in more depth are directed to the links that appear at the foot of this article. To summarise some of the most salient points, however:

  • XForms documents are device- and platform-independent.
  • XForms uses a declarative syntax, which aids faster development and reduces the potential for bugs when compared to more traditional, procedural approaches.
  • XForms separates data, user interface, and application logic according to the model-view-controller (MVC) design pattern.
  • XForms is an open standard, which means that the same document may be deployed unmodified across all conforming processors.
  • XForms documents are inherently accessible by design.
  • XForms enables comprehensive input validation at the point of entry, reducing the need for server round trips in distributed web applications.

Getting Started

Before you can begin, you will need to install Ubiquity formsPlayer, which is a free and a reasonably small download. Additionally, you'll also need to separately install the Ubiquity Browser eXtensions to get the embeddable Renderer component. Once both of these have been installed, you must import two COM libraries into your C# project via the 'Add Reference' dialog:

  • [Program Files]\Common Files\UBX\Renderer\Renderer5.dll
  • [Program Files]\Common Files\UBX\DOM\DOM2Events.dll

The first of these libraries, the Renderer, provides a wrapper for all of the XHTML and XForms rendering functionality. The second component, an implementation of the W3C's DOM Level 2 Events recommendation, facilitates communication between your application, the Renderer, and the live document.

Instantiating the Renderer

Firstly, you must add a new class to your application, representing the window that will host the Renderer. In the example code, this class is called RenHost. The constructor for this class needs to instantiate a copy of RenderLib.RendererClass, storing a pointer to the instance's IRender interface:

C#
public class RenHost : Form {
    private RenderLib.IRender renderer;

    ...

    public RenHost()
    {
        InitializeComponent();

        this.components = new System.ComponentModel.Container();
        this.components.Add(this.buttonOpen);
        this.components.Add(this.labelUrl);
        this.components.Add(this.textboxUrl);
        this.components.Add(this.panelRenderer);

        this.createRenderer();
    }

    private void createRenderer()
    {
        this.renderer = (RenderLib.IRender)new
RenderLib.RendererClass();
    }

    ...
}

Next, this.renderer must be made a child of a Panel on your RenHost class. This is achieved by calling Initialise() and passing in the Panel's handle as a parameter, like so:

C#
private void createRenderer() {
    this.renderer = (RenderLib.IRender)new RenderLib.RendererClass();
    this.renderer.Initialise(null,
(int)this.panelRenderer.Handle);
}

Dispatching Events

Your application may now customise various characteristics of this.renderer, such as setting its initial size or instructing it to delegate any navigation requests to your application. These properties and many others are all set by dispatching events to this.renderer's IEventTarget interface.

To simplify this process, add a new method to your class along the following lines:

C#
private void dispatchEvent(String type, int arg1, int arg2, String arg3) {
    DOM2EventsLib.IRendererEvent rendererEvent = 
	(DOM2EventsLib.IRendererEvent)new DOM2EventsLib.RendererEvent();
    rendererEvent.initRendererEvent(type, false, false, arg1, arg2, arg3);

    DOM2EventsLib.IEventTarget eventTarget = (DOM2EventsLib.IEventTarget)this.renderer;
    eventTarget.dispatchEvent((DOM2EventsLib.DOMEvent)rendererEvent);
}

It is not necessary to worry about what all of the different parameters mean at this point. Simply know that the code above allows you to dispatch a renderer event of the specified type, along with some event-specific contextual parameters.

Initialising your Renderer Instance

The first thing that you will want to do with this new method, then, is to inform this.renderer of its initial size and position:

C#
private void createRenderer() {
    this.renderer = (RenderLib.IRender)new RenderLib.RendererClass();
    this.renderer.Initialise(null, (int)this.panelRenderer.Handle);

    this.dispatchEvent("renderer-set-dimensions",
this.panelRenderer.ClientRectangle.Width,
this.panelRenderer.ClientRectangle.Height, "");
    this.dispatchEvent("renderer-set-position", 0, 0, ""); }

You also need to ensure that this.renderer gets notified of any resizing that occurs in the parent window. This requires you to add a handler for the Resize event:

C#
private void panelRenderer_Resize(object sender, EventArgs evt) {
    this.dispatchEvent("renderer-set-dimensions",
this.panelRenderer.ClientRectangle.Width,
this.panelRenderer.ClientRectangle.Height, ""); }

Once these initial window-related properties have been set, you must instruct this.renderer to take active ownership of its Panel:

C#
private void createRenderer() {
    this.renderer = (RenderLib.IRender)new RenderLib.RendererClass();
    this.renderer.Initialise(null, (int)this.panelRenderer.Handle);

    this.dispatchEvent("renderer-set-dimensions",
this.panelRenderer.ClientRectangle.Width,
this.panelRenderer.ClientRectangle.Height, "");
    this.dispatchEvent("renderer-set-position", 0, 0, "");
    this.dispatchEvent("renderer-activate", 0, 0, "");
}

Rendering Documents

Your renderer instance is now initialised and ready to display your documents. This, in itself, is a two stage process. First, the document must be loaded in one of two ways:

  • By passing a string representation of the markup to LoadFromString()
  • By passing a Microsoft IXMLDOMDocument interface pointer to LoadFromDOM()

Regardless of the method that you use, the document must be well-formed, otherwise the load will fail; malformed documents may only be rendered using the supplementary RenderUnmodified() and NavigateUnmodified() methods. After the document has been loaded successfully, it can be rendered by calling the Render() method. You may also pass a fragment identifier to this method, if necessary.

So, based on the above, a set of methods along the following lines would enable a browser-style application to display a document entered by the user:

C#
private void loadDocument() {
    XmlDocument document = new XmlDocument();
    Uri uri = this.getUri();
    try
    {
        document.Load(uri.AbsoluteUri);
        this.renderer.LoadFromString(document.OuterXml,
this.getBase(uri));
        this.renderer.Render(this.getFragmentId());
    }
    catch (XmlException exception)
    {
        MessageBox.Show(this, "Failed to parse " + exception.SourceUri
+ "\n\nReason: " + exception.Message + "\nLine: " +
exception.LineNumber + "\nColumn: " + exception.LinePosition +
"\nSource: " + exception.Source, "RenHost XML parse error");
    }
    catch (COMException exception)
    {
        MessageBox.Show(this, "The renderer component encountered an error\n" + 
			exception.StackTrace, "RenHost error");
    }
}

private Uri getUri()
{
    if (this.getFragmentIndex() != -1)
        return new Uri(this.textboxUrl.Text.Substring(0,
this.getFragmentIndex()));

    return new Uri(this.textboxUrl.Text); }

private String getBase(Uri uri)
{
    return uri.AbsoluteUri.Substring(0, uri.AbsoluteUri.LastIndexOf('/') + 1); }

private String getFragmentId()
{
    if (this.getFragmentIndex() != -1)
        return this.textboxUrl.Text.Substring(this.getFragmentIndex() + 1);

    return "";
}

private int getFragmentIndex()
{
    return this.textboxUrl.Text.IndexOf('#');
}

Clearing Up

You can continue to load and render as many documents after this as you wish. All that remains is to ensure that you call the Destroy() method before your application terminates:

C#
protected override void Dispose(bool disposing) {
    this.destroyRenderer();

    if (disposing && this.components != null)
    {
        this.components.Dispose();
    }

    base.Dispose(disposing);
}

private void destroyRenderer()
{
    this.renderer.Destroy();
    this.renderer = null;
}

Links

History

  • 09-Oct-2007: Created
  • 13-Nov-2007: Added separate download information for the Renderer component, pending resolution of an issue in the formsPlayer installer
  • 05-Dec-2007: Switched license to CDDL, and made some minor cosmetic changes
  • 11-Feb-2009: Updated code and download details to use latest versions of formsPlayer and UBX

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Software Developer webBackplane
United Kingdom United Kingdom
I'm a Software Engineer, with a background in systems programming using C. In more recent years I have been involved with applications programming, initially using C++ and Delphi, but most recently working with JavaScript and XForms to deliver applications via the web browser. In my spare time I like to play with lots of cool languages that my job doesn't permit me to. Currently, I'm enjoying getting to grips with Erlang.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Naso6417-Jan-11 22:34
Naso6417-Jan-11 22:34 
GeneralRe: My vote of 1 Pin
philbooooooo21-Oct-11 5:08
philbooooooo21-Oct-11 5:08 
QuestionI Have a Question Pin
mshahrouri22-Feb-09 11:10
mshahrouri22-Feb-09 11:10 
AnswerRe: I Have a Question Pin
Phil Booth24-Feb-09 9:51
Phil Booth24-Feb-09 9:51 
Questionevent-based communication between the document and the app Pin
abel_b15-Apr-08 6:11
abel_b15-Apr-08 6:11 
AnswerRe: event-based communication between the document and the app [modified] Pin
Phil Booth15-Apr-08 22:45
Phil Booth15-Apr-08 22:45 
GeneralRe: event-based communication between the document and the app Pin
abel_b16-Apr-08 9:29
abel_b16-Apr-08 9:29 
GeneralRe: event-based communication between the document and the app Pin
Phil Booth17-Apr-08 0:19
Phil Booth17-Apr-08 0:19 
GeneralRe: event-based communication between the document and the app Pin
abel_b17-Apr-08 7:08
abel_b17-Apr-08 7:08 
GeneralRe: event-based communication between the document and the app Pin
Phil Booth18-Apr-08 0:29
Phil Booth18-Apr-08 0:29 
GeneralRe: event-based communication between the document and the app [modified] Pin
Phil Booth18-Apr-08 1:52
Phil Booth18-Apr-08 1:52 
GeneralRe: event-based communication between the document and the app Pin
abel_b18-Apr-08 5:11
abel_b18-Apr-08 5:11 
GeneralRe: event-based communication between the document and the app Pin
Phil Booth18-Apr-08 5:44
Phil Booth18-Apr-08 5:44 
GeneralRe: event-based communication between the document and the app Pin
Phil Booth20-Apr-08 4:11
Phil Booth20-Apr-08 4:11 
GeneralRe: event-based communication between the document and the app Pin
abel_b4-Jun-08 4:38
abel_b4-Jun-08 4:38 
GeneralRe: event-based communication between the document and the app Pin
Phil Booth4-Jun-08 22:49
Phil Booth4-Jun-08 22:49 
GeneralRe: event-based communication between the document and the app Pin
abel_b5-Jun-08 6:13
abel_b5-Jun-08 6:13 
GeneralRe: event-based communication between the document and the app Pin
abel_b27-Jun-08 10:27
abel_b27-Jun-08 10:27 
GeneralRe: event-based communication between the document and the app Pin
abel_b8-Aug-08 3:42
abel_b8-Aug-08 3:42 
GeneralRe: event-based communication between the document and the app Pin
abel_b22-Sep-08 4:55
abel_b22-Sep-08 4:55 
GeneralRe: event-based communication between the document and the app Pin
abel_b6-Feb-09 2:40
abel_b6-Feb-09 2:40 
GeneralRe: event-based communication between the document and the app Pin
Phil Booth11-Feb-09 5:53
Phil Booth11-Feb-09 5:53 
GeneralRenHost.resx is missing Pin
massi6528-Feb-08 22:50
massi6528-Feb-08 22:50 
GeneralRe: RenHost.resx is missing Pin
Phil Booth28-Feb-08 23:01
Phil Booth28-Feb-08 23:01 
GeneralRe: RenHost.resx is missing Pin
massi6528-Feb-08 23:10
massi6528-Feb-08 23:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.