gpt4 book ai didi

c# - 我该如何解决 "Unrecognized element ' elementName'。 (x 行)(x 行)”?

转载 作者:行者123 更新时间:2023-11-30 14:19:54 24 4
gpt4 key购买 nike

我有以下代码

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()
{
}
}

配置是自定义类的一个实例,它有一个名为 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,您可能需要完全实现配置部分类。所以你会得到类似下面的东西:

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# - 我该如何解决 "Unrecognized element ' elementName'。 (x 行)(x 行)”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1985047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com