Reposted from XML forum. This probably has more to do with control building then XML.
I have a feeling I'm missing something simple here. I cannot get even the most simple WebControl to be serialized into an XML file. I keep getting an error "System.Web.UI.WebControls.WebControl cannot be serialized because it does not have a default public constructor. ", although it appears my WebControl does indeed have a default public constructor. Here is my control code.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Xml.Serialization;
using System.IO;
namespace SerializeTest
{
[
ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>"),
Serializable
]
public class MyControl : System.Web.UI.WebControls.WebControl
{
public MyControl():base()
{
}
private string text;
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
protected override void Render(HtmlTextWriter output)
{
output.Write(Text);
}
public void SaveConfiguration(string filepath)
{
XmlSerializer serializer = new XmlSerializer(typeof(MyControl));
TextWriter writer = new StreamWriter(filepath);
serializer.Serialize(writer, this);
writer.Close();
}
}
}
And here is the code of my test application
MyControl1.Text = "blah";
MyControl1.SaveConfiguration("C:\\Temp\\MyConfig.xml");
Thanks for any info/feeback.