Thursday, August 21, 2008

Using Emacs and nXML to edit XAML files

In this post I'll show how to use Emacs and nXML to edit XAML files.

The powerful nXML mode allows the use a RELAX NG schema to validate and to assist the edition of XML documents.

The RELAX NG schema generated in the previous post is used to provide XAML code completion.

As described in the previous post, not all elements of XAML could be mapped to RELAX NG, so validation errors will be displayed on valid documents.

Once you have Emacs and nXML installed the following line could be added to the .emacs file to activate nXML when a XAML file is opened.

(setq auto-mode-alist (cons '("\.xaml$" . nxml-mode) auto-mode-alist))

Also the mode could be activated by typing M-x nxml-mode.

Once a file is opened with the nXML mode, the schema must be specified for that file. The XML -> Set Schema -> File... option is used to specify the silverlight.rnc file created for the previous post.

Setting the XAML schema

Once the schema is load we can start modifying the file. For example the following screenshot shows the result of pressing Control+Enter (C-Return) inside a DoubleAnimation tag.

Presenting available attributes

All the available attributes are presented as possible options.

In several scenarios the schema presents only the valid options for a given context. For example the following screenshot shows the result of pressing Control+Enter after an opening angle bracket inside a StackPanel tag:

Emacs displaying possible options for a StackPanel child

Properties elements are also available, for example the following screenshot shows, available options by pressing Control+Enter after the "<Line." text:

Property Element completion

Code for this post can be found here.