업데이트 : 사용자 지정 구성이 HARD! >. < XML에 대해 몇 가지 배웠습니다. 분명히 엘리먼트에 CDATA를 저장하는 것은 불가능하기 때문에이 질문을 수정 된 구조에 맞게 수정했습니다. 명확성을 위해 질문하십시오. 나는이 문제를 한 달 동안 계속해서 해결해 왔으며 계속해서 여러 문제에 직면 해있다.사용자 지정 구성 섹션의 여러 요소
나는 아래에 언급 된 기사에서 MSDN 예를 따라 왔지만 내 경우에는 개념을 적용하는 데 어려움이 있습니다. 내 프로젝트는 VS2010의 VB.NET 4.0 웹 응용 프로그램이지만 C#의 대답도 괜찮습니다 (VB에서 솔루션이 작동하는 한).
전화 :
Dim conf As ApiSection = CType(ConfigurationManager.GetSection("remoteServiceApi/site"), ApiSection)
예외 (ConfigurationErrorsException)
The element <site> may only appear once in this section.
는 다음과 같은 관련 코드를 유의하시기 바랍니다 ...
스키마 : 여기에 관련 코드입니다 :
<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>
위의 내용을 web.config 파일의 스키마 세트에 추가하고 활성화했는데 실제로 작동하지 않는 것 같습니다. :(이 문제가 될 수
의 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>
클래스 파일 : 나는 가이드로 사용하고
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
참고 :
- How to: Create Custom Configuration Sections Using ConfigurationSection
- Creating Custom Configurations는 당신은
Site
요소를 (당신의 게시의 Web.config에 따라) 컬렉션을 만들 필요가