use*_*554 5 c# xml-serialization
我有一个C#应用程序.在这个应用程序中,我有一些看起来像这样的XML:
string xml = @"<list name=""Groceries"">
<add key=""1"" value=""Milk"" />
<add key=""2"" value=""Eggs"" />
<add key=""3"" value=""Bread"" />
</list>";
Run Code Online (Sandbox Code Playgroud)
我正在尝试将此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;
}
}
Run Code Online (Sandbox Code Playgroud)
当我跑步时,var list = List.Deserialize(xml);我得到了一个List物体.List正确设置的名称.但是,该Items物业是null.为什么不进行Items反序列化?如何Items填充列出的键/值对?
谢谢
小智 3
看起来与之前的问题重复重复。
以下是更正:
System.Configuration.ConfigurationErrorsException:“属性“Items”不得从属性的 get 方法返回 null。通常,getter 应返回 base[""]。'
get
{
var items = base[""] as KeyValueConfigurationCollection;
return items;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
最终版本现如下:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
215 次 |
| 最近记录: |