代码之家  ›  专栏  ›  技术社区  ›  Serhat Ozgel

如何解决“无法识别的元素'elementName'(第x行(第x行)”?

  •  3
  • Serhat Ozgel  · 技术社区  · 16 年前

    我有以下代码

    var section = new CustomConfigurationSection();
    section.SectionInformation.Type = "System.Configuration.NameValueFileSectionHandler";
    section.SectionInformation.SetRawXml(sectionXml);
    configuration.Sections.Add(sectionName, section);
    

    最后一行抛出:

    配置错误异常是一个错误 执行配置时发生错误

    除了内部例外:

    1) (第1行)

    public class CustomConfigurationSection: ConfigurationSection
    {
        public CustomConfigurationSection()
        {
        }
    }
    

    configuration是自定义类的实例,该类具有名为Sections的属性,其类型为“ConfigurationSectionCollection”。

    sectionXml中传入的xml是:

    <monitor>
      <screens>
        <screen>
          <regions>
            <region>
              <labelCoordinates />
              <startupApplication>Internet</startupApplication>
              <color />
              <width>426</width>
              <height>266</height>
              <x1>0</x1>
              <x2>0</x2>
              <y1>0</y1>
              <y2>0</y2>
            </region>
          </regions>
          <height>800</height>
          <width>1280</width>
        </screen>
        <screen>
          <regions />
          <height>0</height>
          <width>0</width>
        </screen>
      </screens>
    </monitor>
    

    我怎样才能让它工作?

    1 回复  |  直到 16 年前
        1
  •  7
  •   Joshua Cauble    16 年前

    在看了您的示例之后,我可以看出它不起作用的一个原因是您正在使用NameValueFileSectionHandler。如果我没记错的话,这只允许使用以下语法:

    <YourSectionName>
      <add key="monitor.region.x" value="0"/>
    <YourSectionName>
    

    基于您想要使用的xml,您可能需要完全实现config节类。因此,您将有如下内容:

    class ServiceResponseSection : ConfigurationSection
    {
        [ConfigurationProperty("ServiceResponses")]
        [ConfigurationCollection(typeof(ServiceResponse), AddItemName = "addServiceResponse", RemoveItemName = "removeServiceResponse", ClearItemsName = "clearServiceResponses")]
        public ServiceResponses ServiceResponses
        {
            get { return this["ServiceResponses"] as ServiceResponses; }
        }
    
    }
    
    public class ServiceResponses : ConfigurationElementCollection
    {
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }
    
        public ServiceResponse this[int index]
        {
            get
            {
                return (ServiceResponse)this.BaseGet(index);
            }
            set
            {
                if (this.BaseGet(index) != null)
                {
                    this.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }
    
        public new ServiceResponse this[string responseString]
        {
            get
            {
                return (ServiceResponse)this.BaseGet(responseString);
            }
            set
            {
                if (this.BaseGet(responseString) != null)
                {
                    this.BaseRemoveAt(this.BaseIndexOf(this.BaseGet(responseString)));
                }
                this.BaseAdd(value);
            }
        }
    
        public void Add(ServiceResponse ServiceResponse)
        {
            this.BaseAdd(ServiceResponse);
        }
    
        public void Clear()
        {
            this.BaseClear();
        }
    
        protected override ConfigurationElement CreateNewElement()
        {
            return new ServiceResponse();
        }
    
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ServiceResponse)element).ResponseString;
        }
    
        public void Remove(ServiceResponse element)
        {
            BaseRemove(element.ResponseString);
        }
    
        public void Remove(string responseString)
        {
            BaseRemove(responseString);
        }
    
        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }
    
    }
    
    public class ServiceResponse : ConfigurationElement
    {
        private int m_tryCount;
    
        public ServiceResponse()
        {
            this.m_tryCount = 0;
        }
    
        [ConfigurationProperty("responseString")]
        public string ResponseString
        {
            get { return (String)this["responseString"]; }
            set { this["responseString"] = value; }
        }
    
        [ConfigurationProperty("matchWholeString")]
        public bool MatchWholeString
        {
            get
            {
                return (bool)this["matchWholeString"];
            }
            set
            {
                this["matchWholeString"] = value.ToString();
            }
        }
    
        [ConfigurationProperty("retryCount")]
        public int RetryCount
        {
            get
            {
                return (int)this["retryCount"];
            }
            set
            {
                this["retryCount"] = value.ToString();
            }
        }
    
        [ConfigurationProperty("failsProcess")]
        public bool FailsProcess
        {
            get
            {
                return (bool)this["failsProcess"];
            }
            set
            {
                this["failsProcess"] = value.ToString();
            }
        }
    
        public int TryCount
        {
            get { return this.m_tryCount; }
            set { this.m_tryCount = value; }
        }
    
        public void Reset()
        {
            this.m_tryCount = 0;
        }
    
    }
    

        <ServiceResponseList>
        <ServiceResponses>
            <clearServiceResponses/>
            <addServiceResponse responseString="API Server Login Error" matchWholeString="false" retryCount="5" failsProcess="false"/>
        </ServiceResponses>
    </ServiceResponseList>
    

    主要的一点是,我使用的是一个集合(在您的示例中,实际上是其中的两个)。在该集合中有一个我正在表示的对象。因此,为了让它解析您拥有的xml,您必须创建一个节处理程序来匹配您正在使用的内容。

    <monitor>
      <screens>
        <screen height="800" width="1280">
          <regions>
             <region startupApplication="Internet" width="426" height="266" x1="0" x2="0" y1="0" y2="0"/>
         </regions>
       </screen>
      </screens>
    </monitor>
    

    然后可以使用类似于我的示例的类。虽然您可能会得到您想要的,但您需要使用其他配置项以使其以这种方式工作。

    推荐文章