求ASP.net中TextBox控件序列化的正确方法!
先看了builder.com.cn上的一篇文章
http://www.builder.com.cn/2006/1224/347743.shtml
但是XmlSerializer serializer = new XmlSerializer(typeof(TextBox));
这一句会出错。
后来又看了MSDN上的一篇文章http://www.microsoft.com/china/msdn/archives/library/dndotnet/html/objserializ.asp
自己改了一下代码,自定义继承了一个TextBox
[Serializable]
public class XMLTextBox : TextBox,ISerializable
{
public XMLTextBox() :base()
{
}
protected XMLTextBox(SerializationInfo info, StreamingContext context)
{
UnitConverter uc = new UnitConverter();
ID = info.GetString( "ID ");
Attributes.Add( "name ", info.GetString( "Name "));
TextMode = (TextBoxMode)info.GetInt32( "TextMode ");
Width = (Unit)uc.ConvertFromInvariantString(info.GetString( "Width "));
Height = (Unit)uc.ConvertFromInvariantString(info.GetString( "Height "));
}
#region ISerializable 成员
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue( "ID ",this.ID);
info.AddValue( "Name ", this.Attributes[ "name "]);
info.AddValue( "TextMode ", this.TextMode);
info.AddValue( "Width ", this.Width.ToString());
info.AddValue( "Height ",this.Height.ToString());
}
#endregion
}
但是XmlSerializer serializer = new XmlSerializer(typeof(TextBox));这一句仍然出错!
改用MSDN上那个IFormatter和流的方法