Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Serialization and ViewState

Serialization and ViewState

Scheduled Pinned Locked Moved C#
helpxmljsonannouncement
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fayu
    wrote on last edited by
    #1

    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; }
    
    F 1 Reply Last reply
    0
    • F Fayu

      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; }
      
      F Offline
      F Offline
      Fayu
      wrote on last edited by
      #2

      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.

      OriginalGriffO 1 Reply Last reply
      0
      • F Fayu

        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.

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        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 have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        F 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          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.

          F Offline
          F Offline
          Fayu
          wrote on last edited by
          #4

          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?

          OriginalGriffO 1 Reply Last reply
          0
          • F Fayu

            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?

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            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.

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups