XML Tools in Visual Studio 2008 XML is everywhere from XML Web Services to databases to config files to Office documents. This article will show you tooling support offered in Visual Studio 2008 that will make working with XML easier. It will cover editing XML files, working with XML schemas, debugging XSLT style sheets and extending Visual Studio by writing your own custom XML Designers. When you open a file with an XML extension in Visual Studio 2008 (for example, .xml, .xsd, .xslt, svg, or .config), you will invoke its XML Editor. XML Editor comes with a full range of features you would expect from a Visual Studio editor, which includes IntelliSense, color-coding, brace matching, outlining, and formatting. It provides full XML 1.0 syntax checking, end-tag completion, as well as DTD and XML schema support with real-time validation. | " | XML Editor comes with a full range of features you would expect from a Visual Studio editor, which includes IntelliSense, color-coding, brace matching, and formatting.
| " |
Editing XML Files Face it, manual editing of XML files can be very tedious and time consuming. To help with this, the Visual Studio 2008 XML Editor comes with a number of productivity enhancement features. One such feature is an extensible library of XML code snippets-XML files that contain a configurable code segment, which acts as a template to use while editing documents. Visual Studio installs a number of XML code snippets that help developers to write XML schemas and XSLT style sheets. To invoke a snippet while editing an XML file, select “Insert Snippet” from the “Edit > IntelliSense” menu. Once you have inserted a snippet, you can TAB between highlighted modifiable fields to enter data. Figure 1 shows an example of inserting a snippet.  Figure 1: Invoking an XML snippet in Visual Studio 2008.You can also write your own snippets. If you want to create a simple snippet, follow these easy steps: - Create a new XML file and type in:
<snippet
- Press ESCAPE to close the IntelliSense window.
- Press TAB.
- Fill in the blanks.
- Press ENTER to finish.
For more detailed information about creating snippets, I recommend VSEditor’s blog post “Code Snippet - Schema Description” (http://blogs.msdn.com/vseditor/archive/2004/07/14/183189.aspx). So what do you do when there are no snippets and you need to create an XML file based on an existing schema? XML Editor offers a wide range of features when you associate your XML documents with XML schemas. Schema-based IntelliSense, real-time validation, and error reporting are just a few of them. In addition, XML Editor can dynamically generate snippets based on an existing schema. Once you provide a name of the element you want to add, the XML Editor can parse the schema for required information, generate a snippet, and insert it for you. To invoke dynamic snippet functionality, all you need to do is type the name of the element as in the following example: <element_name
and press TAB. The XML Editor will create a snippet, which looks very much like the one in Figure 1 except this time you didn’t have to do anything up front. This is a very powerful feature, especially when you need to create documents with large content models. By default the XML Editor generates only the required content, but this can be customized by annotating your XML schemas. More information is available on MSDN under, “How to: Generate an XML Snippet From an XML Schema”. Schema Cache and Schema Catalogs For advanced users, XML Editor offers features such as schema cache and schema catalog files. Schema cache is a folder that contains a number of well-known W3C schemas, as well as a number of Microsoft-specific schemas. It serves as a repository of widely used schemas that are unlikely to change. You’ll find the default location for the schema cache at %vsinstalldir%\xml\schemas, where “%vsinstalldir%” is a variable representing the location in which Visual Studio itself was installed. When you declare one of the namespaces defined by these schemas in your XML files, XML Editor will automatically associate appropriate schemas from the cache location and instantly provide you with IntelliSense and validation. Schema catalogs are XML files located in the Schema Cache directory (catalog.xml file is the default). They give advanced users more granular control over various namespaces they might want to use. For example, you can associate specific namespaces with external locations: <Schema href="mylocation/myschema.xsd" targetNamespace="http://myschema"/>
You can also use catalog files to create associations between schema files and file extensions, which you can find particularly useful when your schema has no targetNamespace: <Association extension="config" schema="xml/schemas/dotNetConfig.xsd"/>
New in Visual Studio 2008, you can also add conditions: <Association extension="config" schema="xml/schemas/dotNetConfig30.xsd" condition="%TargetFrameworkVersion% = 3.0" />
This condition means that the dotNetConfig30.xsd schema should only be associated when the current project is targeting .NET Framework version 3.0. Finally, you can create a chain by pointing one catalog file at another: <Catalog href="http://mycompany/catalog.xml"/>
Working with Large Files Another important editing feature I would like to highlight is the XML Editor’s support for editing large files. While you could work with large files in previous versions, Visual Studio 2008 supports incremental parsing of the XML documents. Now if you work with a 10 MB file, you don’t have to wait for Visual Studio to parse the entire file every time you make an edit. The XML Editor will isolate the edits and reparse only what’s needed, offering better performance and responsiveness. I have covered a few interesting features of XML Editor, but obviously couldn’t go over all of them. Table 1 shows some of the other features available to Visual Studio users. | & | | 
By: Stan Kitsis
Stan Kitsis is a Program Manager in the Data Programmability team at Microsoft. Stan has over 10 years of software development experience. His primary focus is on the XML tools, which include XML Editor, XSLT Debugger, and XML Schema Designer. Prior to working on the XML tools, Stan was responsible for parts of the System.xml and MSXML APIs.
stan.kitsis@microsoft.com | Fast Facts | | “Visual Studio seems to be the best editor for XML (compared to Eclipse plugins and NetBeans) and I use it even when my Web project is Java based.” | |
|