[英]C# - Xml Deserialization of KeyValueConfigurationCollection
我有一个C#应用程序。 在这个应用程序中,我有一些看起来像这样的XML:
string xml = @"<list name=""Groceries"">
<add key=""1"" value=""Milk"" />
<add key=""2"" value=""Eggs"" />
<add key=""3"" value=""Bread"" />
</list>";
我正在尝试将此XML转换为C#对象。 我的班级看起来像这样:
public class List : ConfigurationElement, IXmlSerializable
{
[ConfigurationProperty("name", IsRequired = true, IsKey = true, DefaultValue = "")]
public string Name
{
get { return (string)(this["name"]); }
set { this["name"] = value; }
}
[ConfigurationProperty("", IsRequired = false, IsKey = false,
IsDefaultCollection=true)]
public KeyValueConfigurationCollection Items
{
get
{
var items = base["items"] as KeyValueConfigurationCollection;
return items;
}
set
{
if (base.Properties.Contains("items"))
{
base["items"] = value;
}
else
{
var configProperty = new ConfigurationProperty("items", typeof(KeyValueConfigurationCollection), value);
base.Properties.Add(configProperty);
}
}
}
public XmlSchema GetSchema()
{
return this.GetSchema();
}
public void ReadXml(XmlReader reader)
{
this.DeserializeElement(reader, false);
}
public void WriteXml(XmlWriter writer)
{
this.SerializeElement(writer, false);
}
public static List Deserialize(string xml)
{
List list= null;
var serializer = new XmlSerializer(typeof(List));
using (var reader = new StringReader(xml))
{
list = (List)(serializer.Deserialize(reader));
}
return list;
}
}
当我运行var list = List.Deserialize(xml);
我得到一个List
对象。 List
的名称已正确设置。 但是, Items
属性为null
。 为什么Items
不被反序列化? 如何获取使用列出的键/值对填充的Items
?
谢谢
看起来像早期问题重复。
以下是更正:
System.Configuration.ConfigurationErrorsException:'属性'Items'不能从属性的get方法返回null。 通常,getter应返回base [“”]。
get
{
var items = base[""] as KeyValueConfigurationCollection;
return items;
}
public static List Deserialize(string xml)
{
var serializer = new XmlSerializer(typeof(List), new XmlRootAttribute("list"));
List list = null;
var xdoc = XDocument.Parse(xml);
list = (List)serializer.Deserialize(xdoc.CreateReader());
return list;
}
最终版本现为如下:
public class List : ConfigurationElement, IXmlSerializable
{
public List()
{ }
[ConfigurationProperty("name", IsRequired = true, IsKey = true, DefaultValue = "")]
public string Name
{
get { return (string)(this["name"]); }
set { this["name"] = value; }
}
[ConfigurationProperty("", IsRequired = false, IsKey = false,
IsDefaultCollection = true)]
public KeyValueConfigurationCollection Items
{
get
{
var items = base[""] as KeyValueConfigurationCollection;
return items;
}
set
{
if (base.Properties.Contains("items"))
{
base["items"] = value;
}
else
{
var configProperty = new ConfigurationProperty("items", typeof(KeyValueConfigurationCollection), value);
base.Properties.Add(configProperty);
}
}
}
public XmlSchema GetSchema()
{
return this.GetSchema();
}
public void ReadXml(XmlReader reader)
{
this.DeserializeElement(reader, false);
}
public void WriteXml(XmlWriter writer)
{
this.SerializeElement(writer, false);
}
public static List Deserialize(string xml)
{
List list = null;
var serializer = new XmlSerializer(typeof(List), new XmlRootAttribute("list"));
var xdoc = XDocument.Parse(xml);
list = (List)serializer.Deserialize(xdoc.CreateReader());
return list;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.