locked
How do I do XML serialization of classes with static properties in C#?

    Question

  • How do I do XML serialization of classes with static properties in C#?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, April 08, 2009 3:05 AM

Answers

  • Firstly, XML serialization is feasible by using either BinaryFormatter or SoapFormatter.   Please follow these steps to do XML serialization of classes with static properties:

    1.   Let the class with static properties implement ISerializable, and set the class as Serializable.

    2.   Implement the function GetObjectData.  

    3.   Create a new constructor function with the same structure as GetObjectData function.

    During serialization, the class actually calls GetObjectData.  During deserialization time, the class calls the corresponding constructor to create the instance.  

    For further details, please refer to:
    http://msdn.microsoft.com/en-us/magazine/cc301767.aspx

    Here is the class with static properties:

    [Serializable]
        public class MyClass : ISerializable 
        {
            static int? sindex;
            static int? len;
    
            public static int? StartIndex
            {
                get { return sindex; }
                set { sindex = value; }
            }
            public static int? Length
            {
                get { return len; }
                set { len = value; }
            }
    
            public MyClass()
            { }
    
            public void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                info.AddValue("static.sindex", MyClass.sindex, typeof(int));
                info.AddValue("static.len", MyClass.len, typeof(int));
            }
    
            public MyClass(SerializationInfo info, StreamingContext context)
            {
                MyClass.sindex = info.GetInt32("static.sindex");
                MyClass.len = info.GetInt32("static.len");
            }
        }
    

     

    Code Snippet to do serialization and deserialization:

     private void BinarySerialize()
            {
                FileStream fileStream = new FileStream("test.binary", FileMode.Create);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fileStream, myClass);
                fileStream.Dispose();
            }
            private void BinaryDeserialize()
            {
                FileStream fileStream = new FileStream("test.binary", FileMode.Open);
                BinaryFormatter bf = new BinaryFormatter();
                MyClass myClass = (MyClass)bf.Deserialize(fileStream);
                MessageBox.Show(MyClass.StartIndex.HasValue.ToString());
                MessageBox.Show(MyClass.StartIndex.Value.ToString());
            }
            private void SoapSerialize()
            {
                FileStream fileStream = new FileStream("test.soap", FileMode.Create);
                SoapFormatter sf = new SoapFormatter();
                sf.Serialize(fileStream, myClass);
                fileStream.Dispose();
            }
            private void SoapDeserialize()
            {
                FileStream fileStream = new FileStream("test.soap", FileMode.Open);
                SoapFormatter sf = new SoapFormatter();
                MyClass myClass = (MyClass)sf.Deserialize(fileStream);
                MessageBox.Show(MyClass.StartIndex.HasValue.ToString());
                MessageBox.Show(MyClass.StartIndex.Value.ToString());
            }
    

     

    Related thread:
    http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/687d1222-404e-4009-974e-9189280faa96

     

    For more FAQ about Visual C# General, please see Visual C# General FAQ

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, April 08, 2009 3:07 AM

All replies

  • Firstly, XML serialization is feasible by using either BinaryFormatter or SoapFormatter.   Please follow these steps to do XML serialization of classes with static properties:

    1.   Let the class with static properties implement ISerializable, and set the class as Serializable.

    2.   Implement the function GetObjectData.  

    3.   Create a new constructor function with the same structure as GetObjectData function.

    During serialization, the class actually calls GetObjectData.  During deserialization time, the class calls the corresponding constructor to create the instance.  

    For further details, please refer to:
    http://msdn.microsoft.com/en-us/magazine/cc301767.aspx

    Here is the class with static properties:

    [Serializable]
        public class MyClass : ISerializable 
        {
            static int? sindex;
            static int? len;
    
            public static int? StartIndex
            {
                get { return sindex; }
                set { sindex = value; }
            }
            public static int? Length
            {
                get { return len; }
                set { len = value; }
            }
    
            public MyClass()
            { }
    
            public void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                info.AddValue("static.sindex", MyClass.sindex, typeof(int));
                info.AddValue("static.len", MyClass.len, typeof(int));
            }
    
            public MyClass(SerializationInfo info, StreamingContext context)
            {
                MyClass.sindex = info.GetInt32("static.sindex");
                MyClass.len = info.GetInt32("static.len");
            }
        }
    

     

    Code Snippet to do serialization and deserialization:

     private void BinarySerialize()
            {
                FileStream fileStream = new FileStream("test.binary", FileMode.Create);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fileStream, myClass);
                fileStream.Dispose();
            }
            private void BinaryDeserialize()
            {
                FileStream fileStream = new FileStream("test.binary", FileMode.Open);
                BinaryFormatter bf = new BinaryFormatter();
                MyClass myClass = (MyClass)bf.Deserialize(fileStream);
                MessageBox.Show(MyClass.StartIndex.HasValue.ToString());
                MessageBox.Show(MyClass.StartIndex.Value.ToString());
            }
            private void SoapSerialize()
            {
                FileStream fileStream = new FileStream("test.soap", FileMode.Create);
                SoapFormatter sf = new SoapFormatter();
                sf.Serialize(fileStream, myClass);
                fileStream.Dispose();
            }
            private void SoapDeserialize()
            {
                FileStream fileStream = new FileStream("test.soap", FileMode.Open);
                SoapFormatter sf = new SoapFormatter();
                MyClass myClass = (MyClass)sf.Deserialize(fileStream);
                MessageBox.Show(MyClass.StartIndex.HasValue.ToString());
                MessageBox.Show(MyClass.StartIndex.Value.ToString());
            }
    

     

    Related thread:
    http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/687d1222-404e-4009-974e-9189280faa96

     

    For more FAQ about Visual C# General, please see Visual C# General FAQ

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, April 08, 2009 3:07 AM
  • Introduction and Reference:

    Serialization is the process of converting an object into a form that can be readily transported.  For example, you can serialize an object and transport it over the Internet using HTTP between a client and a server.  On the other end, deserialization reconstructs the object from the stream.

    XML serialization serializes only the public fields and property values of an object into an XML stream.  XML serialization does not include type information.   

    For a detailed introduction, please refer to
    http://msdn.microsoft.com/en-us/library/182eeyhh(VS.80).aspx

    MSDN magazine articles by Jeffrey Richter:
    http://msdn.microsoft.com/en-us/magazine/bb985952.aspx
    http://msdn.microsoft.com/en-us/magazine/cc301767.aspx
    http://msdn.microsoft.com/en-us/magazine/cc188950.aspx

    Here are some helpful tutorials on how to serialize an object in XML via Visual C#:
    http://support.microsoft.com/kb/815813
    http://www.dotnetjohn.com/articles.aspx?articleid=173



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, April 08, 2009 3:15 AM