1

업데이트 : 사용자 지정 구성이 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 

참고 :

답변

1

(그래서 조심스럽게 다음, 2004 년 출판). 이 클래스를 사용하면 요소의 여러 인스턴스를 가지고 있도록 SiteElement을 바꿈됩니다

[ConfigurationCollection(typeof(SiteElement)[ 
public class SiteElementCollection : ConfigurationElementCollection 

    public SiteElement this[string name] 
    { 
     get 
     { 
      return (SiteElement)base.BaseGet(name); 
     } 
    } 

    public SiteElement this[int index] 
    { 
     get 
     { 
      return (SiteElement)base.BaseGet(index); 
     } 
    } 

    public override ConfigurationElement CreateNewElement() 
    { 
     return new SiteElement(); 
    } 

    public override object GetElementKey(ConfigurationElement element) 
    { 
     return ((SiteElement)element).AddressKey; 
    } 

: (죄송 C#에서) 같은 것을보십시오.

다음 섹션에서는 섹션 바로 아래에서 ConfigurationElementCollection을 사용한 적이 없으므로 100 % 확실하지는 않지만 다음 섹션에서는 컬렉션을 참조해야합니다.

예를 들어 (어떤 일을하고 있지만 잘 맞지는 않지만) 섹션 그룹 ​​아래에 섹션 이름이 Sites이라고 가정합니다. 다음과 같이 할 수 있습니다 :

public SiteElementCollection Sites 
{ 
    get 
    { 
     return (SiteElementCollection)base["site"]; 
    } 
} 

적어도 다음과 같은 지침이 있기를 바랍니다.여기서 핵심은 주어진 요소의 인스턴스가 여러 개 필요할 때 ConfigurationElementCollection을 사용하는 것입니다.