无法在C#中反序列化XML – InvalidOperationException

我有一个C#应用程序,在App.config文件中有一个自定义配置信息部分。 目前,我可以通过代码成功加载自定义信息。 但是,我也试图从数据库加载相同的配置信息。 为了做到这一点,我从App.config文件中获取了一串XML,我知道它正在工作。 这个XML字符串如下所示:

          

我试图将此XML反序列化为C#对象。 我已经定义了这样的对象:

Departments.cs

 public class Departments : ConfigurationSection { private Departments() { } [ConfigurationProperty("", IsRequired = false, IsKey = false, IsDefaultCollection = true)] public DepartmentItemCollection Items { get { var items = base[""] as DepartmentItemCollection; return items; } set { base["items"] = value; } } public static Departments Deserialize(string xml) { Departments departments = null; var serializer = new XmlSerializer(typeof(Departments)); using (var reader = new StringReader(xml)) { departments = (Departments)(serializer.Deserialize(reader)); } return departments; } } [ConfigurationCollection(typeof(Department), CollectionType = ConfigurationElementCollectionType.BasicMapAlternate)] public class DepartmentItemCollection : ConfigurationElementCollection { private const string ItemPropertyName = "department"; public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.BasicMapAlternate; } } protected override string ElementName { get { return ItemPropertyName; } } protected override bool IsElementName(string elementName) { return (elementName == ItemPropertyName); } protected override object GetElementKey(ConfigurationElement element) { return ((Department)element).Name; } protected override ConfigurationElement CreateNewElement() { return new Department(); } public override bool IsReadOnly() { return false; } } 

Department.cs

 public class Department : ConfigurationElement { public Department() { } [ConfigurationProperty("id", IsRequired = false, IsKey = true)] public int Id { get { return (int)(this["id"]); } set { this["id"] = value; } } [ConfigurationProperty("name", IsRequired = true, IsKey = true, DefaultValue = "")] public string Name { get { return (string)(this["name"]); } set { this["name"] = value; } } [ConfigurationProperty("products", IsRequired = false, IsKey = false, IsDefaultCollection = false)] public ProductCollection Products { get { return ((ProductCollection)(base["products"])); } set { base["products"] = value; } } } 

DepartmentProducts.cs

 [ConfigurationCollection(typeof(Product), AddItemName = "product", CollectionType = ConfigurationElementCollectionType.BasicMapAlternate)] public class ProductCollection: ConfigurationElementCollection { public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.BasicMapAlternate; } } protected override string ElementName { get { return string.Empty; } } protected override bool IsElementName(string elementName) { return (elementName == "product"); } protected override object GetElementKey(ConfigurationElement element) { return element; } protected override ConfigurationElement CreateNewElement() { return new Product(); } protected override ConfigurationElement CreateNewElement(string elementName) { var product = new Product(); return product; } public override bool IsReadOnly() { return false; } } 

DepartmentProduct.cs

 public class Product : ConfigurationElement { public Product() { } [ConfigurationProperty("name", IsRequired = true, IsKey = true, DefaultValue = "")] public string Name { get { return (string)(this["name"]); } set { this["name"] = value; } } [ConfigurationProperty("price", IsRequired = false)] public decimal Price { get { return (decimal)(this["price"]); } set { price["name"] = value; } } [ConfigurationProperty("", IsRequired = false, IsKey = false, IsDefaultCollection = true)] public KeyValueConfigurationCollection Items { get { var items = base[""] as KeyValueConfigurationCollection; return items; } set { base["items"] = value; } } } 

当我将上面显示的XML传递给Departments.Deserialize方法时,我收到以下错误:

InvalidOperationException:您必须在System.Configuration.ConfigurationLockCollection上实现默认访问器,因为它inheritance自ICollection。

如何将我共享的XML反序列化为共享的C#对象?

我过去也遇到过类似的问题。 虽然我无法弄清楚如何处理InvalidOperationException ,但我设法通过直接将类标记为IXmlSerializable来使其工作

  [XmlRoot("departments")] public class Departments : ConfigurationSection, IXmlSerializable { //Your code here.. public XmlSchema GetSchema() { return this.GetSchema(); } public void ReadXml(XmlReader reader) { this.DeserializeElement(reader, false); } public void WriteXml(XmlWriter writer) { this.SerializeElement(writer, false); } }