31

We have a custom section in my app.config file related to our IoC container class. How can I get intellisense when editing the config file for this section, as well as getting rid of the compiler messages informing me of the missing schema.

I found this question here: app.config configSections custom settings can not find schema information, but I don't understand if it applies to my problem or not, and how to use the answer there if it does.

I also found this page How to get Intellisense for Web.config and App.config in Visual Studio .NET, but it says to remove the xmlns attribute before running the application. Is that really the only/best way?

Here is an example of a simple file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="ServiceContainers"
        type="LVK.IoC.RegistrationsSectionHandler, LVK"/>
  </configSections>
  <ServiceContainers>
    <Registration type="DatabaseConnection" class="DatabaseConnection">
      <Parameter name="connectionString" type="System.String"
          value="TYPE=MSSQL2000;SERVER=localhost;DATABASE=db"/>
    </Registration>
  </ServiceContainers>
</configuration>

Basically I would like to be able to type <R inside the <ServiceContainers> node, and get Registration suggested to me in the intellisense dropdown, as well as the appropriate attributes for it.

Community
  • 1
  • 1
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825

2 Answers2

21

XML Intellisense will not automatically work for a custom configuration section.

Visual Studio may report warnings on compilation complaining that the attributes of the custom configuration section are not defined. These warnings may be ignored.

If you want XML IntelliSense support for a custom configuration section (or if you just want the 'schema not found' warnings to disappear), add the following line to your DotNetConfig.xsd file immediately after the first <xs:schema ...> line (which is typically the second line in the DotNetConfig.xsd file).

<xs:include schemaLocation="YOUR_DIRECTORY\namespace.assemblyname.xsd"/>

The DotNetConfig.xsd file can be found in your Visual Studio 8 (or 9) installation directory in the Xml\Schemas subdirectory.

Steven A. Lowe
  • 60,273
  • 18
  • 132
  • 202
  • 1
    +1. Schemas are often overlooked when it comes to custom configuration sections. They are incredibly useful for when you admin or co-lo has to make changes to either your app.config or web.config. – Joseph Ferris Dec 19 '08 at 14:59
  • 2
    I've got a better solution. Might require VS 2010, not sure :) http://stackoverflow.com/questions/1127315/how-do-i-make-an-extension-xsd-for-the-web-app-config-schema/7977168#7977168 – Merlyn Morgan-Graham Nov 02 '11 at 08:01
  • 4
    is there anyway to generate the "YOUR_DIRECTORY\namespace.assemblyname.xsd"? Or do I just have to write it manually? – NSjonas Feb 13 '13 at 20:08
  • 1
    Editing individual dev machine settings for a feature that should exist is insane. There is already a tool to generate XSD documents from a custom configuration section type (http://www.ookii.org/Software/ConfigurationDocumentationGenerator), and according to this answer: http://stackoverflow.com/a/7681132/88409, you can simply include the XSD in your solution, then mark your section element with the corresponding xmlns attribute. – Triynko Sep 14 '15 at 23:47
  • @Triynko: I agree, it's insane. Note that this answer is from 2008. I would hope there's a better solution now :) – Steven A. Lowe Sep 16 '15 at 03:57
4

If you don't want to modify your DotNetConfig.xsd you could add the xsd configuration "inline".

In your case add the following attributes to the custom section

<ServiceContainers xmlns="your_xmlns"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="your_xmlns location_of_your_schema">

         <Registration ....

</ServiceContainers>

This is useful while testing an xsd locally because location_of_your_schema could be a local path and when you are ready to production change location_of_your_schema to the public url of the xsd file.

Note that the xsi:schemaLocation attribute must contain pairs of strings separated by spaces where the first string in each pair is a namespace URI and the second string is the location of a schema.

Juan M. Elosegui
  • 6,471
  • 5
  • 35
  • 48
  • 1
    Hi, I know this is a year ago. I tried this but I get `Unrecognized attribute 'xmlns:xsi'. Note that attribute names are case-sensitive.` when I run my app. My app runs if I remove the attributes you mentioned, but then I lose Intellisense. Would you know why it causes runtime error? Thanks, in advance – Frank Fajardo Sep 28 '15 at 06:43