XMLSerializer

  • Thread starter Thread starter Eugene Vtial
  • Start date Start date
E

Eugene Vtial

I have been reading the docs on this but I am just not getting it.

Can anyone provide a small sample of using this class to save a simple
Button to a file and then recreate it from the file?
 
Can anyone provide a small sample of using this class to save a simple
Button to a file and then recreate it from the file?
Appearantly you can't serialize a button:

"The property 'Site' on type 'System.ComponentModel.Component' cannot be
serialized because it is decorated with declarative security permission
attributes. Consider using imperative asserts or demands in the property
accessors."

Below is the code I tried.

Greetings,
Wessel


private string SerializeButton(Button button)
{
StringWriter oWriter = new System.IO.StringWriter();
XmlSerializer oSerialize = new XmlSerializer(typeof(Button));
oSerialize.Serialize(oWriter, button);
return oWriter.ToString();
}

private Button DeserializeButton(string sXML)
{
Button oButton = new Button();
StringReader oReader = new System.IO.StringReader(sXML);
XmlSerializer oSerialize = new XmlSerializer(typeof(Button));
return (Button) oSerialize.Deserialize(oReader);
}

private void button1_Click(object sender, EventArgs e)
{
string sXML = SerializeButton(button1);
StreamWriter sw = new StreamWriter(new
FileStream(@"c:\test.xml", FileMode.Create, FileAccess.Write));
sw.Write( sXML );
sw.Close();

button1.Text = "Temp Text";

StreamReader sr = new StreamReader(new
FileStream(@"c:\test.xml", FileMode.Open, FileAccess.Read));
sXML = sr.ReadToEnd();
sr.Close();
button1 = DeserializeButton(sXML);
}
 
Back
Top