XML Editor and Code Snippets

Introduction

How many times in your coding life have you had to write or copy-paste code that was almost identical to something that you’d done before? For example, when working on a new xml schema, how many times did you have to write things like

<xs:complexType name="fooType">

  <xs:sequence>

    <xs:element name="…"/>

      …

  </xs:sequence>

</xs:complexType>

I know, I know, you can use graphical Schema Editors where you can just point-and-click to generate this code. But can you point-and-click to generate code for a complex type with a sequence of three elements and an xs:any as in the following example?

<xs:complexType name="fooType">

  <xs:sequence>

    <xs:element name="element1"/>

    <xs:element name="element2"/>

    <xs:element name="element3"/>

    <xs:any namespace="##other" processContents="strict"/>

  </xs:sequence>

</xs:complexType>

No? I didn’t think so.

The XML Editor is going to help you with this. VS2005 introduced a new and very powerful feature called Code Snippets. A code snippet is an XML file (with a .snippet extension) that contains a chunk of code in which you can configure different parameters, reference assemblies, and use references and import statements. Think of them as templates that you can configure to meet your needs. The XML Editor supports this feature and also adds dynamically generated code snippets based on your XML Schemas.

Standard (Static) Snippets

Static snippets are available not only in XML Editor, but also in C# and VB. They are created by you or somebody else and stored as .snippet files for future use. When you install XML Editor, you get a number of snippets that we have created for you. For example, a snippet for a new simple type with a pattern restriction:

<xsd:simpleType  name="name" xmlns:xsd="https://www.w3.org/2001/XMLSchema">

  <xsd:restriction base="xsd:string">

    <xsd:pattern value=" "/>

  </xsd:restriction>

</xsd:simpleType>

or a C# script block to use in your xslt:

<ms:script implements-prefix="user" xmlns:ms="urn:schemas-microsoft-com:xslt" language="C#" >

    double my_code() {

     

    }

</ms:script>

To see a list of all available snippets, go to the Tools menu, select Code Snippets Manager, and then select XML from the dropdown. To insert a snippet, right-click and select “Insert snippet…” (also available from Edit | Intellisense | Insert Snippet or Ctrl+K Ctrl+X). If you are using beta2, you might need to hit SPACE to bring up a list of available snippets. Once you’ve inserted a snippet, you can tab between various modifiable parameters of the snippet, which will be highlighted. The snippet will be “committed” when you hit Enter. Also, note that each snippet can have a shortcut, which you can use to insert it. For example, “stpattern” is a shortcut for the “simple type with a pattern restriction” snippet shown above. To use this shortcut, type “<stpattern” (no closing “>”) and hit TAB. Make sure that before you hit TAB, intellisense drop-down window is not shown (you can hit ESCAPE if it is).

You can also write your own static snippets. All you need to do is to create an xml file in accordance to the schema for snippets, which is installed with VS. There are multiple articles that describe how to create your own snippets for VS. For example, “Code Snippet – Schema Description” by Sean Laberee. However, what these articles fail to mention is that we provide you with a “Snippet for Snippets”. That’s right, all you need to do is create a new XML file, type “<snippet”, hit TAB and fill in the blanks. Now you can create your own libraries of snippets and even share them with your friends and co-workers.

Dynamic Snippets

Dynamic snippets, unlike static snippets, are a unique feature of the XML Editor and are only applicable when working with instance documents that have a schema associated with them. Let’s say you are creating an xml document for a purchase order which contains order ID, customer name, product name, order date, shipping address, and a bunch of other elements. While XML Editor helps you with this by providing intellisense options, it is still a lot of typing. Now in order to provide you with intellisense, the Editor had to parse the schema and at this point knows perfectly well what the structure of the purchase order is. So why not just print it there and let you fill in the values? And that’s exactly what dynamic snippets are. You invoke them in a manner similar to the static snippets – simply type TAB after the element name (for example, “<purchaseOrder”) to get all required attributes and child content pre-populated for you.

Try them out and let me know what you think.