Serialize subclass of windows.forms.label
-
I have a subclass of Windows.Forms.Label that I'd like to serialize to XML without using IXmlSerializable. A windows control (a Label in this case) isn't serializable. Consider the following...
public class SubLabel : Windows.Forms.Label
{
public Text {get; set;}
public int LocationX {get; set;}
public int LocationY {get; set;}public void Save(string path)
{
XmlSerializer xs = new XmlSerializer(typeof(SubLabel));
using (StreamWriter sw = new StreamWriter(path))
{
xs.Serialize(sw, this);
}
}public static SubLabel Load(string path)
{
XmlSerializer xs = new XmlSerializer(typeof(TestClass));
using (StreamReader sr = new StreamReader(path))
{
return (SubLabel)xs.Deserialize(sr);
}
}
...
}All I want to serialize are the public properties above. I'd like to keep it simple by using the Save() and Load() methods above. Of course this fails because of the Label parent class. Is there an easy way to do this?
-
I have a subclass of Windows.Forms.Label that I'd like to serialize to XML without using IXmlSerializable. A windows control (a Label in this case) isn't serializable. Consider the following...
public class SubLabel : Windows.Forms.Label
{
public Text {get; set;}
public int LocationX {get; set;}
public int LocationY {get; set;}public void Save(string path)
{
XmlSerializer xs = new XmlSerializer(typeof(SubLabel));
using (StreamWriter sw = new StreamWriter(path))
{
xs.Serialize(sw, this);
}
}public static SubLabel Load(string path)
{
XmlSerializer xs = new XmlSerializer(typeof(TestClass));
using (StreamReader sr = new StreamReader(path))
{
return (SubLabel)xs.Deserialize(sr);
}
}
...
}All I want to serialize are the public properties above. I'd like to keep it simple by using the Save() and Load() methods above. Of course this fails because of the Label parent class. Is there an easy way to do this?
You could copy the selected public properties (
Text
,LocationX
,LocationY
) to a separate class and just serialize that. YourLoad()
method would then deserialize that class and return a newSubLabel
with those properties set. /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com