Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get intellisense in app.config for a custom section?

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.

like image 837
Lasse V. Karlsen Avatar asked Dec 18 '08 15:12

Lasse V. Karlsen


People also ask

How do I add a config section in app config?

Open the App. config file and add the configSections, sectionGroup and section to it. We need to specify the name and fully qualified type of all the section and section group.

How can I add app config file in console application in Visual Studio 2019?

In Solution Explorer, right-click the project node, and then select Add > New Item. The Add New Item dialog box appears. Expand Installed > Visual C# Items. In the middle pane, select the Application Configuration File template.

Does app config get compiled?

Now you might be wondering what happens behind the scenes. Well, when you compile your application, the compiler actually copies the app. config file to the output folder, but gives it another name: When you start your application (ConsoleApp1.exe in our example), the matching config file will be loaded too.

Where is app config stored?

The application configuration file usually lives in the same directory as your application. For web applications, it is named Web. config. For non-web applications, it starts life with the name of App.


2 Answers

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.

like image 80
Steven A. Lowe Avatar answered Sep 19 '22 12:09

Steven A. Lowe


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.

like image 33
Juan M. Elosegui Avatar answered Sep 21 '22 12:09

Juan M. Elosegui