Using the Windows Forms XML Parser Sample
by Joe Stegman
Table of Contents
Introduction
Basic Sample
Markup Parser Reference
Introduction
This is a sample of an extensible mechanism to add a markup model on top of an existing .NET Framework object model. This sample’s parsing rules can be summarized as “XML elements map to .NET Framework types and XML attributes map to Type properties (or events)”. This sample includes a markup parser that dynamically generates an object instance tree from an XML file. The markup format includes constructs for the following:
- XML Namespace to .NET Framework Namespace mapping
- Object Instancing
- Object Identification and References
- Property Sets
- Instance and Static method invocation
- Event Wire-Ups
- Assembly References
Disclaimer
This is only a sample and will not be part of the next version of Windows Forms. In addition, this sample was written against the .NET Framework version 1.1 and has not been tested against other versions.
Basic Sample
The following sample shows the XML syntax used to declare a simple Windows Forms Form with a single child Label Control.
xml version="1.0" encoding="utf-8" ?>
mapping xmlns="http://www.microsoft.com/2003/WindowsForms"
namespace="System.Windows.Forms;System.Drawing"?>
<wfml xmlns="http://www.microsoft.com/2003/WindowsForms"
xmlns:wfml="http://www.microsoft.com/2003/WFML">
<Form wfml:root="true" Text="Basic Sample" Size="300,200">
<Label Text="Hello World" AutoSize="True" Location="10, 20"/>
<method.Show/>
Form>
wfml>
The Windows Forms XML parser sample will generate an instance tree (Form) from the above XML. This assumes the above XML is contained in a file named “sample.xml”.
MarkupParser parser = new MarkupParser();
object form = parser.Parse("basic.xml");
This will dynamically generate the following Form:
Dissecting the Basic Sample
The basic sample is composed of an XML declaration, a Windows Forms parser specific processing instruction, a root tag, the instance declarations and an end tag. These sections are described below:
XML Declaration
xml version="1.0" encoding="utf-8" ?>
This is a standard XML declaration that describes the XML version and encoding. This line is at the top of most XML files and is not specific to this sample.
Sample Processing Instruction
mapping xmlns="http://www.microsoft.com/2003/WindowsForms"
namespace="System.Windows.Forms;System.Drawing"?>
This line defines a mapping between an XML namespace and .NET Framework namespaces. This line declares that any elements defined in the XML namespace “http://www.microsoft.com/2003/WindowsForms” will map to types in the .NET Framework namespaces “System.Windows.Forms or System.Drawing”. See the Markup reference section for more information on the mapping processing instruction.
Root Tag
<wfml xmlns="http://www.microsoft.com/2003/WindowsForms"
xmlns:wfml="http://www.microsoft.com/2003/WFML">
This line defines the default XML namespace for the sample file (“http://www.microsoft.com/2003/WindowsForms”) and defines “wfml” as a prefix for elements and attributes in the “http://www.microsoft.com/2003/WFML” namespace.
Instance Declarations
<Form wfml:root="true" Text="Basic Sample" Size="300,200">
<Label Text="Hello World" AutoSize="True" Location="10,20"/>
<method.Show/>
Form>
When processing this XML, the parser will create an instance of type Form using the default Form constructor and set its Text property to “Basic Sample” and its Size property set to “300,200”. The parser will create a “Label” and add it to the Form’s Controls collection (see below for details) and set the Label’s Text property to “Hello World”, set the Label’s AutoSize property to “True” and set the Label’s Location property to “10,20”. Finally, the parser will call the method “Show” on the Form instance.
XML Namespace to .NET Framework Mappings
The sample parser creates an instance of the System.Windows.Forms.Form type as a result of parsing the