tags:

views:

365

answers:

3

I am currently saving a .net ( c# ) usercontrol to the disk as a XML file by saving each property as an element in the xml document. The file is used for later recreation of the controls at runtime. I am wondering if it is possible or better to save the control as a binary file. There would be many controls so I guess it would have to have a header section describing the location and length of each saved controls. Thoughts?

Brad

BTW this is a windows app

EDIT:

what I currently have inplace is a public member function that uses the propertyDescriptior class to itinerate through all the properties and create an xml document from that.

     PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(this);

        for (int i = 0; i <= pdc.Count - 1; i++)
        {   pdc[i].Name
  pdc[i].PropertyType
 pdc[i].Category

}

I will look into creating the class Serializable - thanks

+1  A: 

Winforms controls don't serialize especially well, and you might have a lot of difficulty getting the base-classes (i.e. not your code) to play ball. Things like Color, for example, regularly provide surprisingly troublesome to serialize.

Xml would be an obvious (if somewhat predictable) choice, but you generally need to nominate sub-classes ahead of time. And of course, the base-classes won't be marked serializable. BinaryFormatter would avoid some of that, but as a field-based serializer, you'd have problems with the "handles" etc in the base-classes, which are meaningless serialized.

I'm not saying it can't be done - but it won't be trivial either. As a starter, you'd want to look at TypeConverter.GetProperties, and use the Converter of each to get the value as an invariant string.

Marc Gravell
thanks. I have had the XML option working for some time but I was day dreaming as to other methods os saving I could use.
Brad
I am using PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(this); foreach (PropertyDescriptor pd in pdc)to collect all the properties I needed for recreation.
Brad
+1  A: 

We had to do this for a data-driven application where a user could create persistable views. We did an XML version to start but moved to using BinaryFormatter and the ISerializable interface as this allows us to control exactly what gets persisted and which constructors to use. For the controls we actually persisted the CodeCompileUnit that the designer has created, but that means you have to actually use a designer to lay them out.

Jeff Yates
+1 for ISerializable.
Daniel Schaffer
A: 

just had a thought: maybe you dont need to serialize the base/sub-classes. maybe you could write another searializer that only serializes the top tier of the class inheritance hierarchy? only serializing your classes (you wrote) and maybe storing meta-data for the base classes you may derive from (so that you can re-map this on de-serialization)?? this could just be pie-in-the-sky too.

cottsak