21

How do I make a schema for custom config sections? I tried making one, but when I used it, it said the only expected element was what I had in that schema, and complained about the standard web.config stuff, even though I was still using the normal DotNetConfig.xsd file too.

Max Schmeling
  • 12,363
  • 14
  • 66
  • 109

3 Answers3

39

This question I found isn't duplicate, but the solution will solve your problem:

How to fix Error: "Could not find schema information for the attribute/element" by creating schema

The trick is to get the "Properties" of the app.config editor, and set the Schemas value:

  • Right Click -> Properties anywhere in the XML file editor, or just hit F4 while it is in focus
  • In that dialog, add a local or absolute reference to a schema file

My app.config file's properties window/gadget looks like this:

Properties dialog in Visual Studio for the app.config file

Here's an example I just got working (I'm toying around with Ninject and NLog). The elements and attributes under the nlog section show up correctly in Intellisense, and I get squiggly lines if I violate the schema.

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="eventLog" xsi:type="EventLog" log="Application"
              category="TestService" />
      <target name="file" xsi:type="File"
              layout="${longdate}|${stacktrace}|${message}"
              fileName="${logger}.txt" />
    </targets>
    <rules>
      <logger name="*" minlevel="Info" writeTo="eventLog" />
      <logger name="*" minlevel="Debug" writeTo="file"/>
    </rules>
  </nlog>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
</configuration>

My schema file is in my project root, right next to app.config, and called NLog.xsd. I simply saved it from here:

Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • 1
    For some reason, this only got me 98% of the way there. While most compiler warnings went away, I got one new one telling me that the "configuration" element was already defined. After looking at the sample NLog.xsd above, I realized I had more "stuff" in my xsd and once I trimmed it down to only include _my schema_ and not the entire schema of the config file, it started working as expected. – Jim Jun 21 '12 at 16:55
  • Worked like a charm. Thanks for posting! – fourpastmidnight Sep 21 '12 at 19:18
1

Maybe it's just my environment or something changed in .NET 4.6 (not sure).

To get intellisense to work with a newly created app.config file...

Step 1 : Add a new item App.Config to your solution.
It will look like this, note the intellisense errors:

AppConfigs

Step 2 : Press F4 in the editor to show the Properites page of the XML document:

My defaults were showing this:

Properties

Step 3: Click the elipse at far right of Schemas property above...

Check the DonNetConfig.xsd, close the window and start typing in

Intellisense

No more errors and intellisense works...

JWP
  • 6,672
  • 3
  • 50
  • 74
-1

When I tried this, it didn't work. The configuration system assumes that everything is in the default namespace, and chokes if it's not. It's very disappointing.

John Saunders
  • 160,644
  • 26
  • 247
  • 397