Deserialize a ConfigurationElement derived class myself ?

S

Softlion

Hi,
I've a class MyElement which represents a configuration element, so as .NET
2 it derives from ConfigurationElement.
I would like to create an instance of MyElement using a XML stream I loaded
from an external source (which is not a configuration file).

I tryed to use XmlSerializer like this :
XmlSerializer mySerializer = new XmlSerializer( typeof(
Settings.MyElement ) );
StreamWriter myWriter = new StreamWriter( @"c:\MyElement.xml" );
mySerializer.Serialize( myWriter, myObjectOfTypeMyElement );
myWriter.Close();

But it won't work complaining about the public field
ConfigurationLockCollection from the ConfigurationElement class not being
serializable !!

{System.InvalidOperationException: There was an error reflecting type
'App.MyElement'. ---> System.InvalidOperationException: You must implement a
default accessor on System.Configuration.ConfigurationLockCollection because
it inherits from ICollection.
at System.Xml.Serialization.TypeScope.GetDefaultIndexer(Type type, String
memberInfo).
......

Do I have to implement my own IXmlSerializer on the class ? But will it
still work as a configuration element ? Any other mean to do that ?
What a bad idead to have to derive from a framewrok class for the
configuration !!!! Microsoft team should change that again in the next
version.
 
S

Softlion

Ok I've implemented the ISerializable interface and it works with both Xml
serialization and Configuration element serialization.

Thanks Microsoft teams for helping us NOT programming 2 times the same thing
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top