How to configure the Visual Studio 2005 IDE to use custom XSD files for IntelliSense

I've finally gotten some time to experiment with some of the features of the Visual Studio 2005 IDE (as you can see from my post last week about how to populate the Add References dialog). I wanted to share a couple of tricks I discovered about using Visual Studio 2005 to create and edit XML that I found pretty useful, but for which the documentation was either vague or lacking (in my opinion).

I have been trying to install or register an XSD file so that Visual Studio 2005 can find it and automatically use it when I am editing specific types of XML documents in the IDE. Initially, I expected that there would be a simple registry-based solution to associate new XSDs like there is for populating folders with assemblies into the Add References dialog. After some research, I couldn't find a way to do this so I began looking for what kind of other options are available.

I found an MSDN document that describes what is new in code editing in VS 2005. This document describes some of the features of the new XML editor in the IDE. I was specifically interested in "Flexible schema association" and "XSD-based IntelliSense" so I tried to find more information about these topics. I ended up finding this topic about the schema cache. Based on this document, I was able to get the following mechanisms to work to cause Visual Studio 2005 to recognize my XSD:

Option 1 - copy the XSD into the Visual Studio 2005 schemas directory

  • Place a copy of the XSD file into the folder %ProgramFiles%\Microsoft Visual Studio 8\XML\Schemas

I also decided to associate a file extension for my file type with Visual Studio 2005. If you are using a well-known file extension you may not need to use these additional steps.

  • Go to the Tools menu and choose Options
  • If you are using a Visual Studio 2005 Express Edition, check the box in the lower left corner named Show all settings
  • Expand the Text Editor tree item and choose File Extension
  • Type the name of your extension, choose XML Editor in the dropdown and click OK

This option worked fine for me, but required that I copy my XSD into the Visual Studio 2005 schemas directory.

Option 2 - modify the Visual Studio 2005 schemas catalog.xml file

  • Edit %ProgramFiles%\Microsoft Visual Studio 8\XML\Schemas\catalog.xml and add a new section that looks like the following:
    <Schema href="(path to your XSD file)" targetNamespace="(your schema namespace)" />

Because I also wanted to associate a file extension for my file type with Visual Studio 2005, I added this line to catalog.xml as well. If you are using a well-known file extension you may not need to add this additional entry.

<Association extension="(your extension)" schema="(path to your XSD file)"/>

This option allowed me to store my XSD file in whatever location I wanted to, but had the drawback of requiring me to edit one of the configuration files that ships with Visual Studio 2005.

Option 3 - add a new XML file to the Visual Studio 2005 schemas directory

I could not find this option documented on MSDN, but I discovered it by asking some questions of the team that developed the XML Editor features in Visual Studio 2005. I wanted to be able to register my schema without requiring my XSD file to be in the central Visual Studio 2005 schemas directory and without requiring any modifications to the catalog.xml file that shipped with Visual Studio 2005. I found out that Visual Studio 2005 will parse files named *.xml in %ProgramFiles%\Microsoft Visual Studio 8\XML\Schemas and look for additional schema registration, even if the file is not named catalog.xml.

So in this option, I did the following:

  • Create a new file named myschema.xml
  • Add the following lines to the XML file:
    <Schema href="(path to your XSD file)" targetNamespace="(your schema namespace)" />
    <Association extension="(your extension)" schema="(path to your XSD file)"/>
  • Copy the file to %ProgramFiles%\Microsoft Visual Studio 8\XML\Schemas

This option allowed me to store my XSD file in whatever location I wanted to, and it did not require me to to edit one of the configuration files that ships with Visual Studio 2005. This is the option I ended up choosing for my scenario.

Additional notes

A few other notes about this process that I found while trying to figure this out:

  • The MSDN documentation claims that Visual Studio monitors the %ProgramFiles%\Microsoft Visual Studio 8\XML\Schemas directory when the IDE is running and will dynamically update if any changes are made. I may have been doing something wrong, but I could not get that feature to work and I had to close and reopen the IDE in order to see the changes I made to the contents of the Schemas directory get picked up and used by Visual Studio
  • The MSDN documentation also claims you can add a new <catalog> entry in %ProgramFiles%\Microsoft Visual Studio 8\XML\Schemas\catalog.xml in order to add new directories that Visual Studio will look in to try to find XSD files. I could not get that functionality to work either, but I feel that option 3 listed above is cleaner anyways because you don't have to edit a pre-existing XML file (which makes it easier to write a setup that will register a schema with Visual Studio because Windows Installer does not have native support for editing XML files and you would have to write a custom action to do this)

Also, the coolest thing I discovered while looking into this is that any annotations in your XSD will be picked up and used by Visual Studio 2005 as IntelliSense comments. That means when you type an open angle bracket to start a tag, and you get the dropdown with a list of applicable tags, when you give focus to each tag VS will display tooltips for the tag based on the annotations in the XSD file. This is mentioned very briefly in this MSDN article, but I found this to be extremely useful - once I found out that I could easily enable this feature, I didn't need to Alt+Tab back and forth to the help documentation for my schema nearly as often. Very cool!!