繁体   English   中英

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#对象。 我的班级看起来像这样:

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;
        }
  • 使用root属性更新了序列化程序:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM