39

When editing .NET config files (app.config, web.config, etc) in Visual Studio, I get Visual Studio's intellisense to guide me when choosing my application's settings. If I add a custom configuration section, how can I enable intellisense for my custom settings? I'm sure there must be an easy answer to this, but a cursory Google search didn't give me any help.

Thanks!

Kevin Babcock
  • 10,187
  • 19
  • 69
  • 89

3 Answers3

35

As the other answers say, you need to provide an XML Schema document for your custom configuration section. There's no need to add the .xsd schema file to some global directory; you can reference it directly from your custom section in the App.config file:

<configuration>

  <!-- make the custom section known to .NET's configuration manager -->
  <configSections>
    <section name="customSection" type="..." />
  </configSections>

  <!-- your custom section -->
  <customSection xmlns="http://tempuri.org/customSection.xsd"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:noNamespaceSchemaLocation="customSection.xsd">
    ...
  </customSection>

<configuration>

The xmlns attribute is merely there to set a default namespace, so that you don't need to set it on your customSection element and all of its child elements. (However, do not place the xmlns attribute on the <configuration> element!)

The customSection.xsd contains the schema that will be used by IntelliSense, for example:

<xs:schema id="customSectionSchema"
           targetNamespace="http://tempuri.org/customSection.xsd"
           elementFormDefault="qualified"
           xmlns="http://tempuri.org/customSection.xsd"
           xmlns:mstns="http://tempuri.org/customSection.xsd"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="customSection">
    ...
  </xs:element>
</xs:schema>
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • What TYPE am I supposed to put in section.type? I guessed and put "xmlns" in there and it works...but I'm pretty sure that's wrong. – Prisoner ZERO Feb 15 '11 at 14:28
  • 1
    @Prisoner, you're supposed to put a (at least assembly-qualified) .NET type name à la `"Namespace.Class, Assembly"` there. The framework will instantiate this type and use it whenever it wants to parse the custom configuration section. Choose any of a few pre-defined section handler classes, or any custom class that implements the `IConfigurationSectionHandler` interface. For further info, google for something like `"App.config custom section handlers"`, or read an article on the topic such as [this Code Project article](http://www.codeproject.com/KB/aspnet/ConfigSections.aspx) to get started. – stakx - no longer contributing Apr 27 '11 at 20:06
  • I know this is a bit late, but this seems to cause runtime problem when the Configuration Manager tries to read the custom config section and it could not recognize the `xmlns:xsi` attribute. Do you know what I can do to fix it? Thanks – Frank Fajardo Sep 28 '15 at 23:31
  • 1
    @Frank: I've noticed these kinds of problems, too. Did you put the `xmlns:xsi` attribute on your custom section's element, or on an ancestor element (such as ``)? The latter will very likely cause problems. – stakx - no longer contributing Sep 29 '15 at 08:26
  • 1
    @stakx, I had the `xmlns:xsi` on the section element, like your example. And I have the `.xsd` included in my project. It seems to work now (both Intellisense and runtime read) *after* I removed the `xmlns:xsi` and `xsi:noNamespaceSchemaLocation` attributes and just leave the `xmlns` attribute. – Frank Fajardo Sep 29 '15 at 22:17
  • @Frank: That is strange. I don't know what's up with that, but glad to hear you got things working. Thank you for commenting on this issue! – stakx - no longer contributing Sep 30 '15 at 06:56
32

If you do not want to modify your Visual Studio files or copy anything into the Visual Studio folder, you can add the .xsd file to your project, open your .config file and select Schemas in the Properties window (click the […] icon):

Screenshot of Visual Studio showing where to find and change the "Schemas" property of your <code>.config</code> file

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
Michael Stum
  • 177,530
  • 117
  • 400
  • 535
  • 5
    +1 The accepted solution seems widely practiced, but you shouldn't do this unless the schema changes are standard and useful to all the Visual Studio projects that may be created on your computer.(http://msdn.microsoft.com/en-us/library/ms255821.aspx) – Paul Feb 05 '10 at 16:06
11

You need to create an XSD file for your custom settings and copy it to the schema directory of your visual Studio install. For 2005, this is: %ProgramFiles%\Microsoft Visual Studio 8\XML\Schemas

Here some information on this. http://blogs.msdn.com/astebner/archive/2005/12/07/501466.aspx

Jose Basilio
  • 50,714
  • 13
  • 121
  • 117