c# – 如何解决“无法识别的元素’elementName’. (第x行)(第x行)“?

c# – 如何解决“无法识别的元素’elementName’. (第x行)(第x行)“?,第1张

概述我有以下代码 var section = new CustomConfigurationSection();section.SectionInformation.Type = "System.Configuration.NameValueFileSectionHandler";section.SectionInformation.SetRawXml(sectionXml);configura 我有以下代码

var section = new CustomConfigurationSection();section.Sectioninformation.Type = "System.Configuration.nameValuefileSectionHandler";section.Sectioninformation.SetRawXml(sectionXml);configuration.Sections.Add(sectionname,section);

最后一行抛出:

ConfigurationErrorsException An error
occurred executing the configuration
section handler for monitor.

内部异常:

Unrecognized element ‘screens’. (line
1) (line 1)

CustomConfigurationSection的定义:

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>

我怎样才能让它发挥作用?

解决方法 看了你的例子,我可以看到它不起作用的一个原因是你正在使用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;    }}

然后使用xml,如下所示:

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

重点是我正在使用一个集合(在你的例子中,你实际上有几个).在该集合中是我代表的对象.因此,要让它解析你拥有的xml,你必须创建一个部分处理程序来匹配你正在使用的.

根据我的示例,您可能希望将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>

然后,您可以使用类似于我的示例的类.虽然你可能得到你想要的东西,但你需要使用其他配置项来让它以这种方式工作.

希望有所帮助.

总结

以上是内存溢出为你收集整理的c# – 如何解决“无法识别的元素’elementName’. (第x行)(第x行)“?全部内容,希望文章能够帮你解决c# – 如何解决“无法识别的元素’elementName’. (第x行)(第x行)“?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1215732.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存