有没有办法用JSON而不是XML编写配置节?
假设我有以下ConfigurationSection
public class UsersConfig : ConfigurationSection {
[ConfigurationProperty("users",
IsRequired = false)]
public UserCollection Users {
get { return this["users"] as UserCollection; }
}
}
[ConfigurationCollection(typeof(UserElement),
AddItemName = "user"]
public class UsersCollection : ConfigurationElementCollection {
protected override ConfigurationElement CreateNewElement() {
return new UserElement();
}
protected override object GetElementKey(ConfigurationElement element) {
return ((UserElement)element).Name;
}
}
public class UserElement : ConfigurationElement {
[ConfigurationProperty("name",
IsRequired = true,
IsKey = true)]
public string Name {
get { return this["name"] as string; }
set { this["name"] = value; }
}
}
然后,我可以创建以下XML配置节:
<users-config>
<users>
<user name="Matt458" />
<user name="JohnLennon" />
</users>
</users-config>
我想要实现的是保持具有相同的UsersConfig类,但我不想将它映射到XML,而是希望将它映射到JSON:
{
"users": [
{
"name": "Matt458"
},
{
"name": "JohnLennon"
}
]
}
https://stackoverflow.com/questions/30042795
复制相似问题