none
How to serialize and deserialize ConfigurationElement object RRS feed

  • Question

  • Hello,

    I need serialize and desirelize objects of Configuration Manager (http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx).
    This is part of my code:

        [Serializable]
        public class PropertyElement : ConfigurationElement
        {
            public PropertyElement() { }
    
            [ConfigurationProperty("name", IsKey = true)]
            public string Name
            {
                get { return ((string)(base["name"])); }
                set { base["name"] = value; }
            }
    
            [ConfigurationProperty("value")]
            public string Value
            {
                get { return ((string)(base["value"])); }
                set { base["value"] = value; }
            }
        }

    An exception occurs when I try to serialize ConfigurationElement:

    XmlSerializer serializer = new XmlSerializer(typeof(PropertyElement));
    Exception:
    An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
    Additional information: There was an error reflecting type 'CustomConfigLoader.PropertyElement'.
    InnerException: "You must implement a default accessor on System.Configuration.ConfigurationLockCollection because it inherits from ICollection."

    How to serialize and deserialize ConfigurationElement object ?

    Sunday, April 27, 2014 12:02 PM

Answers

  • Hi, you can try to implement IXmlSerializable interface, please see below:

    public class PropertyElement : ConfigurationElement, IXmlSerializable
        {
            public PropertyElement()
            {
            }
    
            [ConfigurationProperty("name", IsKey = true)]
            public string Name
            {
                get
                {
                    return ((string)(base["name"]));
                }
                set
                {
                    base["name"] = value;
                }
            }
    
            [ConfigurationProperty("value")]
            public string Value
            {
                get
                {
                    return ((string)(base["value"]));
                }
                set
                {
                    base["value"] = value;
                }
            }
    
            public System.Xml.Schema.XmlSchema GetSchema()
            {
                return this.GetSchema();
            }
    
            public void ReadXml(XmlReader reader)
            {
                this.DeserializeElement(reader, false);
            }
    
            public void WriteXml(XmlWriter writer)
            {
                this.SerializeElement(writer, false);
            }
        }

    • Marked as answer by mperever Monday, April 28, 2014 6:39 AM
    Sunday, April 27, 2014 1:43 PM

All replies

  • Hi, you can try to implement IXmlSerializable interface, please see below:

    public class PropertyElement : ConfigurationElement, IXmlSerializable
        {
            public PropertyElement()
            {
            }
    
            [ConfigurationProperty("name", IsKey = true)]
            public string Name
            {
                get
                {
                    return ((string)(base["name"]));
                }
                set
                {
                    base["name"] = value;
                }
            }
    
            [ConfigurationProperty("value")]
            public string Value
            {
                get
                {
                    return ((string)(base["value"]));
                }
                set
                {
                    base["value"] = value;
                }
            }
    
            public System.Xml.Schema.XmlSchema GetSchema()
            {
                return this.GetSchema();
            }
    
            public void ReadXml(XmlReader reader)
            {
                this.DeserializeElement(reader, false);
            }
    
            public void WriteXml(XmlWriter writer)
            {
                this.SerializeElement(writer, false);
            }
        }

    • Marked as answer by mperever Monday, April 28, 2014 6:39 AM
    Sunday, April 27, 2014 1:43 PM
  • Hi, you can try to implement IXmlSerializable interface, please see below:

    public class PropertyElement : ConfigurationElement, IXmlSerializable
        {
            public PropertyElement()
            {
            }
    
            [ConfigurationProperty("name", IsKey = true)]
            public string Name
            {
                get
                {
                    return ((string)(base["name"]));
                }
                set
                {
                    base["name"] = value;
                }
            }
    
            [ConfigurationProperty("value")]
            public string Value
            {
                get
                {
                    return ((string)(base["value"]));
                }
                set
                {
                    base["value"] = value;
                }
            }
    
            public System.Xml.Schema.XmlSchema GetSchema()
            {
                return this.GetSchema();
            }
    
            public void ReadXml(XmlReader reader)
            {
                this.DeserializeElement(reader, false);
            }
    
            public void WriteXml(XmlWriter writer)
            {
                this.SerializeElement(writer, false);
            }
        }

    Thanks a lot, very helpful
    Monday, April 28, 2014 6:40 AM