Serialization and ViewState
-
I have a class that is serializable (using the SerializableAttribute). I have also implemented the ISerializable to that class. I am able to serialize/deserialize the object by doing the following:
string xml = string.Empty; System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings(); StringWriter sw = new StringWriter(); settings.OmitXmlDeclaration = true; XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(sw, settings)) { XmlSerializer ser = new XmlSerializer(typeof(Content)); ser.Serialize(writer, this, ns); return sw.ToString(); }
My problem is when I try to bind the object to a ViewState, I get the following error:
Type 'System.Xml.XmlNode' in Assembly 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable
Below are my classes (shortened). Any help is appreciated. Thanks in advance. Content Class:
\[Serializable\] \[DataObject(true)\] public class Content : ISerializable { //Constructors public Content() { } public Content(SerializationInfo info, StreamingContext context) { this.contentCollection = (List<Content> )info.GetValue("ContentCollection", typeof(List<Content> )); this.ContentId = info.GetInt64("ContentId"); this.Name = info.GetString("Name"); this.Owner = (Owner)info.GetValue("Owner", typeof(Owner)); } //Properties private List<Content> contentCollection; \[XmlArray("ContentCollection")\] public List ContentCollection { get { if (contentCollection == null) contentCollection = new List(); return this.contentCollection; } } private System.Int64 contentIdField; \[XmlAttribute("ContentId")\] public System.Int64 ContentId { get { return this.contentIdField; } set { this.contentIdField = value; } } private System.String nameField; \[XmlElement("Name")\] public System.String Name { get { return this.nameField; } set { this.nameField = value; }
-
I have a class that is serializable (using the SerializableAttribute). I have also implemented the ISerializable to that class. I am able to serialize/deserialize the object by doing the following:
string xml = string.Empty; System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings(); StringWriter sw = new StringWriter(); settings.OmitXmlDeclaration = true; XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(sw, settings)) { XmlSerializer ser = new XmlSerializer(typeof(Content)); ser.Serialize(writer, this, ns); return sw.ToString(); }
My problem is when I try to bind the object to a ViewState, I get the following error:
Type 'System.Xml.XmlNode' in Assembly 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable
Below are my classes (shortened). Any help is appreciated. Thanks in advance. Content Class:
\[Serializable\] \[DataObject(true)\] public class Content : ISerializable { //Constructors public Content() { } public Content(SerializationInfo info, StreamingContext context) { this.contentCollection = (List<Content> )info.GetValue("ContentCollection", typeof(List<Content> )); this.ContentId = info.GetInt64("ContentId"); this.Name = info.GetString("Name"); this.Owner = (Owner)info.GetValue("Owner", typeof(Owner)); } //Properties private List<Content> contentCollection; \[XmlArray("ContentCollection")\] public List ContentCollection { get { if (contentCollection == null) contentCollection = new List(); return this.contentCollection; } } private System.Int64 contentIdField; \[XmlAttribute("ContentId")\] public System.Int64 ContentId { get { return this.contentIdField; } set { this.contentIdField = value; } } private System.String nameField; \[XmlElement("Name")\] public System.String Name { get { return this.nameField; } set { this.nameField = value; }
-
Turns out ViewStates does not support Xml Objec serialization. Instead it uses the LosFormatter so BinaryFormatter should work (havent tried yet). However, Session states do support Xml serialization. Suggestions/comments still welcome.
I tend to use binary serialization for several reasons: 1) Size. Binary < XML < Soap If you have a lot of data, the difference can be "significant" and result in quicker load times. For example, Bin: 1.4Mb, XML: 2.5Mb, Soap: 9.4Mb 2) The XmlSerializer type will only serialize public data fields or private data exposed by public properties. Private data not exposed from properties will be ignored. This is way too easy to forget, and can cause some very odd problems later.
-
I tend to use binary serialization for several reasons: 1) Size. Binary < XML < Soap If you have a lot of data, the difference can be "significant" and result in quicker load times. For example, Bin: 1.4Mb, XML: 2.5Mb, Soap: 9.4Mb 2) The XmlSerializer type will only serialize public data fields or private data exposed by public properties. Private data not exposed from properties will be ignored. This is way too easy to forget, and can cause some very odd problems later.
Thanks for your reply. Currently I am receiving an Xml which I an deserializing into a custom class. This custom class contains the [Serializable] attribute and also implements the ISerializable interface (probably extra work i did not need to do). The properties of this class contains the [XmlElement] and [XmlAttribute] attributes. Does this mean that this class can only be serialized/deserialized using the XmlSerializer only? When I attempt to serialize/deserialize using the BinaryFormatter, I get the following error:
Type 'System.Xml.XmlNode' in Assembly 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
I also get this error when trying to set the ViewState item value to the object. I get the feeling that the binarySerializer is trying to serialize an object that is already xmlserialized... Is it possible for me to use mutlitple types of serializers on a single object?
-
Thanks for your reply. Currently I am receiving an Xml which I an deserializing into a custom class. This custom class contains the [Serializable] attribute and also implements the ISerializable interface (probably extra work i did not need to do). The properties of this class contains the [XmlElement] and [XmlAttribute] attributes. Does this mean that this class can only be serialized/deserialized using the XmlSerializer only? When I attempt to serialize/deserialize using the BinaryFormatter, I get the following error:
Type 'System.Xml.XmlNode' in Assembly 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
I also get this error when trying to set the ViewState item value to the object. I get the feeling that the binarySerializer is trying to serialize an object that is already xmlserialized... Is it possible for me to use mutlitple types of serializers on a single object?
If you are receiving an XML serialization, then the only thing you can deserialize it with is an XmlSerializer - it is entirly possible that the error you are getting is a reflection of this. I.e. that the XmlNode entries are private, not available as public properties and thus the tree can be saved by a XmlSerializer, but not with a BinaryFormatter since this will try to save all private variables as well. Since XmlNore does not appear to have a [Serializable]attribute, I think you are stuck with XmlSerializer. You could use multiple type of serializer, but since each would contain the same data this would just increase the effort, without producing a benefit.