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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With