请考虑以下配置部分:
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="xxx.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</sectionGroup>
</configSections>
<applicationSettings>
<xxx.Properties.Settings>
<setting name="xxx_Service1" serializeAs="String">
<value>https://xxx.service1</value>
</setting>
<setting name="xxx_Service1" serializeAs="String">
<value>https://xxx.service2</value>
</setting>
<setting name="xxx_Service1" serializeAs="String">
<value>https://xxx.service3</value>
</setting>
</xxx.Properties.Settings>
</applicationSettings>
我尝试实现 System.Configuration.ClientSettingsSection 的自定义版本。我需要完全相同的层次结构
ConfigSection
- ConfigElement
- ConfigElement
- ConfigElement
基于AssemblyExplorer
的代码,我编写了自定义实现:
{
static void Main()
{
var sec = (CustomClientSettingsSection) ConfigurationManager.GetSection("serviceSettings");
Console.Read();
}
}
public class CustomClientSettingsSection : ConfigurationSection
{
private static readonly ConfigurationProperty _propSettings = new ConfigurationProperty((string)null,
typeof(CustomSettingElementCollection), (object)null, ConfigurationPropertyOptions.IsDefaultCollection);
[ConfigurationProperty("", IsDefaultCollection = true)]
public CustomSettingElementCollection Services
{
get { return (CustomSettingElementCollection)this[CustomClientSettingsSection._propSettings]; }
}
}
public class CustomSettingElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new CustomSettingElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((CustomSettingElement)element).Key;
}
}
public class CustomSettingElement : ConfigurationElement
{
private static readonly ConfigurationProperty _propName = new ConfigurationProperty("name", typeof(string), (object)"", ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey);
internal string Key
{
get
{
return this.Name;
}
}
[ConfigurationProperty("name", DefaultValue = "", IsKey = true, IsRequired = true)]
public string Name
{
get
{
return (string)this[CustomSettingElement._propName];
}
set
{
this[CustomSettingElement._propName] = (object)value;
}
}
}
我正在尝试解析此 config.xml
<configSections>
<section name="serviceSettings" type="ConsoleApplication1.CustomClientSettingsSection, ConsoleApplication1"/>
</configSections>
<serviceSettings>
<setting name="xxx_Service1">
</setting>
<setting name="xxx_Service2">
</setting>
</serviceSettings>
我收到错误消息:
无法识别的元素“设置”。
如何解析此配置并获取具有相同属性的configElement
列表?
答案 0 :(得分:2)
这是我从此处改编的代码,适合您的示例。
Code Project: Custom Configuration Sections in app.config or web.config
This will help you.
JSONArray jsonArrayList = new JSONArray();
ArrayList<String> label=new ArrayList<>();
JSONArray jsonArray = new JSONArray(readlocationFeed);
if(jsonArray.length()>0){
for(int i=0;i<jsonArray.length();i++){
JSONArray jsonArrayForValue = new JSONArray(jsonArray.get(i));
if(i==0){
////////**********For Label/Header*******///////////
for(int j=0;j<jsonArrayForValue.length();j++){
label.add(jsonArrayForValue.get(j));
}
}else{
JSONObject eachDeatail=new JSONObject();
for(int j=0;j<jsonArrayForValue.length();j++){
eachDeatail.put(label.get(j),jsonArrayForValue.get(label.get(j)).toString());
}
jsonArrayList.add(eachDeatail);
}
}
}
csc TestCodeApp.cs
.\TestCodeApp.exe
我对其进行了测试,并且效果很好。让我知道你是否有疑问。