Search code examples
.netconfigurationapp-configconfigurationmanager

.NET How to generate schema for app.config?


Using the ConfigurationManager code/annotation configuration, I can define a rich, typed model for my app.config structure. Is there a way (or tool or special incantation) to generate the corresponding XSD from this code-based model?

Having an XSD-schema makes it awesome to edit configuration files! (never thought I'd say that :)

enter image description here


Solution

  • So far, I found the lovely-looking Configuration Section Designer VS plugin and it generates XSD in addition to code!
    But this is a designer-only solution and lacks a bit of flexibility (plus I'm afraid a designer-based solution is harder to scale).

    LATER EDIT

    I ended up giving Configuration Section Designer a more serious chance and I'm loving it! As it turns out, it's plenty flexible for my purpose. Here's shot of a part of my configuration definition:

    enter image description here

    Pros:

    • Easy to use
    • Generates XSD for the config file
    • Generates a (bare-bones) app.config file
    • Flexible, supports lots of the customizations exposed by ConfigurationManager. Here are some of the ones I used:
      • Enum support (that trickles all the way to the generated XSD, so you get IntelliSense when filling in attribute values
      • 'Default collection' for sections, so you can support <section><item/><item/><section> instead of <section><items><item/><item/></items><section>
      • Out of the box support for type 'System.Type' for attributes (and the associated converter)
      • option to generate strongly-typed element collections (implementing ICollection)
      • input validators, including custom (roll-your-own)
      • Type converters for loading/storing elements with properties of any types, including Roll-your-own
      • Config element inheritance

    I'm sure there are other things that I forgot about.

    A few drawbacks:

    • The documentation is not exhaustive (although it does send you in the right direction)
    • The errors that sometimes show up are not very explicit (along the lines of "something went 'splat' in CodeDom"), but so far it's always been a problem in my model (e.g. I forgot to assign a property).

    But these are minor nuissances.

    Overall, I got the job done quickly and easily. Thumbs up!