gpt4 book ai didi

C# - KeyValueConfigurationCollection 的 Xml 反序列化

转载 作者:太空狗 更新时间:2023-10-29 23:28:34 24 4
gpt4 key购买 nike

我有一个 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# 对象。我的类(class)看起来像这样:

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 填充列出的键/值对?

谢谢

最佳答案

看起来与之前的 question 重复.

这里是更正:

  • 从 getter 中删除了“items”,否则抛出

System.Configuration.ConfigurationErrorsException: 'The property 'Items' must not return null from the property's get method. Typically the getter should return 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;
}
}

关于C# - KeyValueConfigurationCollection 的 Xml 反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51326218/

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