Aaron Stebner's WebLog

Thoughts about setup and deployment issues, WiX, XNA, the .NET Framework and Visual Studio

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

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

  • Comments 18

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!!

 

  • Thanks for posting this! It was very helpful to me.
  • Have you been able to register support for other than DotNetConfig.xsd with app/web.config?

    To me it seems that as soon as you register the schema %ProgramFiles%Microsoft Visual Studio 8\Xml\Schemas\DotNetConfig.xsd it takes over and forgets about any other schema?
  • Hi Niels - I'm not sure what you mean by registering the schema.  I have been able to copy multiple XSD files into the XML\Schemas folder and they work fine within the VS IDE.  I have also been able to drop multiple XML files with pointers to XSDs into that folder and they all work fine.
  • Hi,

    Thanks for those tips.

    I'm currently working on a project that would require support for xForms and I tried your tip to actually provide intellisense to my code.

    However, it doesn't seems to work on the aspx pages... Is there any way to provide intellisense there?

    Philippe (amadrias@hotmail.com)
  • Hi Philippe - I'm sorry, but I'm not familiar with Intellisense for ASPX pages in VS 2005.  I would suggest searching in the MSDN Forums (http://forums.microsoft.com) or on MSDN itself (http:/msdn.microsoft.com) for additional technical references.  I'm sorry I'm not able to be more helpful here.
  • I was able to get Option 3 working, but with an additional tweak. In the 'myschema.xml' file, I had to enclose 'Schema' and 'Association' elements in a 'SchemaCatalog' element, just like it is in catalog.xml (including the namespace).

    FYI, the paths to my external schema file are in the form of 'file://D:/MySchemas/MySchema.xsd'.
  • A couple of days ago, Rob Caron posted about how to author Work Item Types with IntelliSense in Visual...
  • Here&amp;#39;s a handy post on how to enable intellisense for RibbonX XML markup. It just registeres the
  • http://blog.360.yahoo.com/smhmayboudi

    Enjoy the intellisense web.config

    Download this file ( DotNetConfig.xsd ):

    http://www.peterritchie.com/Hamlet/Downloads/Downloads_GetFile.aspx?id=74

    Then put it in this folder:

    C:\Program Files\Microsoft Visual Studio 8\Xml\Schemas\

    Make your web.config with this structure:

    <?xml version="1.0" encoding="utf-8"?>

    <configuration xmlns:xs="DotNetConfig.xsd" xmlns:vs="http://schemas.microsoft.com/.NetConfiguration/v2.0">

    [Write you web configuration here]

    </configuration>

  • One of the local government branches had some questions on Team Foundation Server and I thought others

  • XML Schemas dialog in Visual Studio 2008

  • PingBack from http://www.keyongtech.com/546471-net-2-0-custom-web

  • Adding Custom Intellisense To App.config files

  • have u managed to find out how to use intellisense for xforms in visual studio 2008

Page 1 of 2 (18 items) 12
Leave a Comment
  • Please add 7 and 5 and type the answer here:
  • Post