2013-10-07 1 views
2

누군가 내가 여기서 잘못하고있는 것을 알아낼 수 있기를 바랍니다.ConfigurationCollection - 인식 할 수없는 요소 '항목'

<invalidCharactorGroup> 
<entries> 
    <entry name="entry1" oldChar="É" newChar="E"/> 
    <entry name="entry2" oldChar="B" newChar="C"/> 
</entries> 
</invalidCharactorGroup> 

선언이이 오류 얻을

<sectionGroup name="invalidCharactorGroup"> 
    <section name="entries" 
      type="WSTG.Config.InvalidCharactorSection, WSTGEcomLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 
      allowLocation="true" 
      allowDefinition="Everywhere" />   
</sectionGroup> 

:

public class InvalidCharactorSection : ConfigurationSection 
{ 
    [ConfigurationProperty("entries")] 
    [ConfigurationCollection(typeof(InvalidEntryElementCollection), AddItemName = "entry")] 
    public InvalidEntryElementCollection Entries 
    { 
     get { return ((InvalidEntryElementCollection)(base["entries"])); } 
     set { base["entries"] = value; } 
    } 
} 


public class InvalidEntryElementCollection : ConfigurationElementCollection 
{ 
    internal const string PropertyName = "Entry"; 

    public override ConfigurationElementCollectionType CollectionType 
    { 
     get 
     { 
      return ConfigurationElementCollectionType.BasicMapAlternate; 
     } 
    } 
    protected override string ElementName 
    { 
     get 
     { 
      return PropertyName; 
     } 
    } 

    protected override bool IsElementName(string elementName) 
    { 
     return elementName.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase); 
    } 


    public override bool IsReadOnly() 
    { 
     return false; 
    } 


    protected override ConfigurationElement CreateNewElement() 
    { 
     return new Entry(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((Entry)(element)).name; 
    } 

    public Entry this[int idx] 
    { 
     get 
     { 
      return (Entry)BaseGet(idx); 
     } 
    } 
} 


public class Entry : ConfigurationElement 
{ 
    [ConfigurationProperty("name", DefaultValue = "", IsKey = true, IsRequired = true)] 
    public string name 
    { 
     get { return (string)base["name"]; } 
     set { base["name"] = value; } 
    } 
    [ConfigurationProperty("oldChar", DefaultValue = "", IsKey = false, IsRequired = true)] 
    public string oldChar 
    { 
     get { return (string)base["oldChar"]; } 
     set { base["oldChar"] = value; } 
    } 


    [ConfigurationProperty("newChar", DefaultValue = "", IsKey = false, IsRequired = true)] 
    public string newChar 
    { 
     get { return (string)base["newChar"]; } 
     set { base["newChar"] = value; } 
    } 

} 

답변

2

: 여기

Unrecognized element 'entry'. 

내 클래스입니다을 내 Web.config의에서 사용자 정의 섹션을 PropertyName을 "Entry"로 선언 한 것으로 보입니다. 이는 "진입"과 다릅니다.

나는 실제 코드를 시도

UPDATE, 난 그냥이 하나의 설정 파일에 섹션 그룹 ​​정의를 변경했다 :의 이름

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <!--<sectionGroup name="invalidCharactorGroup"> 
     <section name="entries" 
       type="WSTG.Config.InvalidCharactorSection, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 
       allowLocation="true" 
       allowDefinition="Everywhere" /> 
    </sectionGroup>--> 
    <section name="invalidCharactorGroup" type="WSTG.Config.InvalidCharactorSection, ConsoleApplication1"/> 
    </configSections> 
    <invalidCharactorGroup> 
    <entries> 
     <entry name="entry1" oldChar="É" newChar="E"/> 
     <entry name="entry2" oldChar="B" newChar="C"/> 
    </entries> 
    </invalidCharactorGroup> 
</configuration> 

변경 "의 ConsoleApplication1의" 이진. 다음과 같이 구성을 읽을 수 있어야합니다.

InvalidCharactorSection section = ConfigurationManager.GetSection("invalidCharactorGroup") as InvalidCharactorSection; 

Entry entry = section.Entries[0]; 
+0

PropertyName을 "항목"으로 변경하면 작동하지 않습니다. 같은 오류 – PhillyNJ

+0

당신은 또한이 질문을 체크 아웃 할 수 있습니다 : http://stackoverflow.com/questions/3935331/how-to-implement-a-configurationsection-with-a-configurationelementcollection –