UPDATE: Custom Configs are freaking HARD! >.< While I've learned some things about XML; evidently it's impossible to store CDATA in an element, so I've rephrased this question to fit the modified structure. Please ask me any questions for clarity. I've been working on this issue for around a month off and on and keep running into various issues.
I've been follow the MSDN example in the article referenced below, but I'm have trouble applying the concept to my case. My project is a VB.NET 4.0 Web Application in VS2010, but answers in C# are alright as well (as long as the solution will work in VB). :) Here's the relevant code:
Call:
Dim conf As ApiSection = CType(ConfigurationManager.GetSection("remoteServiceApi/site"), ApiSection)
Exception (ConfigurationErrorsException)
The element <site> may only appear once in this section.
Please note the following relevant code...
Schema:
<xs:element name="ApiSection">
<xs:complexType>
<xs:sequence>
<xs:element name="site" id="environment" nillable="false" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="environment" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Production"></xs:enumeration>
<xs:enumeration value="Sandbox"></xs:enumeration>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="APIKey" type="xs:string" use="required" />
<xs:attribute name="APIUsername" type="xs:string" use="required" />
<xs:attribute name="APIPassword" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
Even though I added and activated the above in the schema set for the web.config file, it does not seem to be actually working. :( Could this be the issue?
Web.Config:
<configSections>
<sectionGroup name="remoteServiceApi">
<section name="site" type="RemoteServiceApi.ApiSection" />
</sectionGroup>
</configSections>
<remoteServiceApi environment="Sandbox">
<site environment="Sandbox"
APIKey="reallyLongHashKeyDev"
APIUsername="samplejoe"
APIPassword="joespass" />
<site environment="Production"
APIKey="reallyLongHashKeyProd"
APIUsername="samplejoe"
APIPassword="joespass" />
</remoteServiceApi>
Class File:
Imports System.Configuration
Namespace RemoteServiceApi
Public Class ApiSection
Inherits ConfigurationSection
<ConfigurationProperty("site", IsRequired:=True)> _
Public Property site() As SiteElement
Get
Return CType(Me("site"), SiteElement)
End Get
Set(value As SiteElement)
Me("site") = value
End Set
End Property
End Class
Public Class SiteElement
Inherits ConfigurationElement
<ConfigurationProperty("Environment", DefaultValue:="Sandbox", IsRequired:=True)> _
Public Property Environment() As String
Get
Return CStr(Me("Environment"))
End Get
Set(value As String)
Me("Environment") = value
End Set
End Property
<ConfigurationProperty("APIKey", IsRequired:=True)> _
Public ReadOnly Property APIKey() As String
Get
Return CStr(Me("APIKey"))
End Get
End Property
<ConfigurationProperty("APIUsername", IsRequired:=True)> _
Public ReadOnly Property APIUsername() As String
Get
Return CStr(Me("APIUsername"))
End Get
End Property
<ConfigurationProperty("APIPassword", IsRequired:=True)> _
Public ReadOnly Property APIPassword() As String
Get
Return CStr(Me("APIPassword"))
End Get
End Property
End Class
Public Enum Environment
Production
Sandbox
End Enum
End Namespace
References I'm using as a guide:
- How to: Create Custom Configuration Sections Using ConfigurationSection
- Creating Custom Configurations (published in 2004, so following cautiously)