partial class DaveKehring : IProgrammer { }  RSS 2.0
# Thursday, November 20, 2008

Intellisense is wonderful. It's so useful it's actually scary because it doesn't force you to remember a lot class names, enum values, etc. This is good for productivity but bad when you need to recall something without the Visual Studio IDE!

In any case, I like to get Intellisense whenever I can. One area that I kept putting off was adding Intellisense for custom configuration sections I create. I dislike loads of <add> tags in the <appSettings> section because it becomes hard to navigate. I prefer to create custom config sections. However, I don't get the benefit - out of the box - of Intellisense and schema validation. To get this, you need to create your own XSD file to support your custom config sections.

XSDs for config sections are not hard to write. In fact, its best to just take a look at the XSD file for the .NET config file which can be found in your Visual Studio installation folder. For example, on my machine for VS 2008, I navigate to:

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

Here you'll find loads of XSD files for all sorts of things. The most important one however is called DotNetConfig.xsd. This contains the schema definitions for the base .NET config file. There are also supplimental files for .NET 2.0 and 3.0 config sections in DotNetConfig20.xsd and DotNetConfig30.xsd respectively.

Here's a simple example of a config section:

<fileRepositories>
   
<fileRepository id="MainLib" isDefault="true" connectionString="\\JUPITER\MainLib" />
</fileRepositories>

The xsd schema (MyConfig.xsd) for this config section is:

<?xml version="1.0" encoding="us-ascii"?>
   
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                    
xmlns:vs=http://schemas.microsoft.com/Visual-Studio-Intellisense 
                     
elementFormDefault="qualified" 
                     
attributeFormDefault="unqualified">

   <xs:element name="fileRepositories">
      <
xs:complexType>
         <
xs:choice minOccurs="0" maxOccurs="unbounded">
            <
xs:element name="fileRepository">
               <
xs:complexType>
                  <
xs:attribute name="id" type="xs:string" use="required" />
                  <
xs:attribute name="isDefault" type="xs:boolean" default="false" use="optional"/>
                  <
xs:attribute name="connectionString" type="xs:string" use="required"/>
               </
xs:complexType>
            </
xs:element>
         </
xs:choice>
      </
xs:complexType>
   </
xs:element>
</
xs:schema>

I placed this file in the ...\Xml\Schemas folder in the Visual Studio installation folder as noted above. Now, to hook this into VS, you need to add it to the xsd catalog. You do this by editing the file catalog.xml. Add the following line to the bottom of this file before the closing <schemaCatalog> tag:

  <Association extension="config" schema="%InstallRoot%/xml/schemas/MyConfig.xsd" condition="%TargetFrameworkVersion% != 2.0 and %TargetFrameworkVersion% != 3.0" />

That's it! Now when you go into VS and edit a config file, you'll get Intellisense!

 

Thursday, November 20, 2008 8:57:11 AM (Eastern Standard Time, UTC-05:00)  #    Comments [1] -
Configuration
Friday, December 12, 2008 4:33:34 PM (Eastern Standard Time, UTC-05:00)
Any idea what the Association conditions are based on and/or what macros and logic it supports? I've been unable to locate any documentation for it.
hemp
Comments are closed.
Navigation
Archive
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Dave Kehring
Sign In
Statistics
Total Posts: 11
This Year: 0
This Month: 0
This Week: 0
Comments: 2
Themes
Pick a theme:
All Content © 2012, Dave Kehring
DasBlog theme 'Business' created by Christoph De Baene (delarou)