C# - Xml KeyValueConfigurationCollection 的反序列化

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# 对象。我的 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 填充列出的 key/value 对?

谢谢

看起来与之前的 重复。

更正如下:

  • 从 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;
    }
}