Kuepper
Kuepper

Reputation: 1004

IntelliSense for Enum in Custom Configuration Section

I want to use an enum in my Custom Configuration Section. So I implemented an enum DatabaseMode and the according property.

I also implemented the according Property in my System.Configuration.ConfigurationElement. But for the IntelliSense to work in the web.config I need to provide the schema definition (xsd) mirroring the same structure in xsd format.

My question is how the schema is supposed to look to support the enum?

The enum with the different options:

public enum DatabaseMode
{
   Development,
   Deployment,
   Production
}

The property storing the information about the Mode:

[ConfigurationProperty(databaseAlias)]
public DatabaseElement Database
{
   get { return (DatabaseElement)this[databaseAlias]; }
   set { this[databaseAlias] = value; }
}

Below the important part of my schema file:

<xs:element name="database">
  <xs:complexType>
    <xs:attribute name="server" type="xs:anyURI" use="required" />
    <xs:attribute name="name" type="xs:string" use="required" />
    <xs:attribute name="user" type="xs:string" use="required" />
    <xs:attribute name="password" type="xs:string" use="required" />
  </xs:complexType>
</xs:element>

Upvotes: 2

Views: 1217

Answers (1)

fourpastmidnight
fourpastmidnight

Reputation: 4234

You can actually define an enumeration in XSD's. For your example DatabaseMode property, the XSD fragment will look like this:

<xs:attribute name="databaseMode"> <!-- I'm assuming you're using camelCasing -->
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="development" />
            <xs:enumeration value="deployment" />
            <xs:enumeration value="production" />
        </xs:restriction>
    </xs:simpleType>
</xs:attribute>

Hope that helps.

A rleated question, in case anyone else wants to answer is, once the XSD is created, where should it be placed so that Visual Studio recognizes it within the web.config file?

UPDATE: I found the answer to my above question here on SO.

Upvotes: 1

Related Questions