locked
XMLSerialization in static properties in C# RRS feed

  • Question

  • I've the following class, all member data are static fields. and I don't know how to for this class. This class store smdr information of a pbx.

        [XmlRoot("PBXDriverSpecification")] 
        public class PBXDriverDef 
        { 
            public PBXDriverDef() 
            { 
     
            } 
     
            public class Outgoing 
            {             
                public class Class 
                { 
                    static string name = null
                    public static string Name 
                    { 
                        get { return name; } 
                        set { name = value; } 
                    } 
                }                        
     
                public class Time 
                { 
                    static string format = null
                    static int? sindex; 
                    static int? len; 
     
                    public static string Format 
                    { 
                        get { return format; } 
                        set { format = value; } 
                    } 
                  
                    public static int? StartIndex 
                    { 
                        get { return sindex; } 
                        set { sindex = value; } 
                    } 
                    public static int? Length 
                    { 
                        get { return len; } 
                        set { len = value; } 
                    } 
                } 
     
                public class Date 
                { 
                    static string format = null
                    static int? sindex; 
                    static int? len; 
     
                    public static string Format 
                    { 
                        get { return format; } 
                        set { format = value; } 
                    } 
     
                    public static int? StartIndex 
                    { 
                        get { return sindex; } 
                        set { sindex = value; } 
                    } 
                    public static int? Length 
                    { 
                        get { return len; } 
                        set { len = value; } 
                    } 
                } 
     
                public class Duration 
                { 
                    static string format = null
                    static int? sindex; 
                    static int? len; 
     
                    public static string Format 
                    { 
                        get { return format; } 
                        set { format = value; } 
                    } 
     
                    public static int? StartIndex 
                    { 
                        get { return sindex; } 
                        set { sindex = value; } 
                    } 
                    public static int? Length 
                    { 
                        get { return len; } 
                        set { len = value; } 
                    } 
                } 
     
                public class Extension 
                { 
                    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 class DialNoCLID 
                { 
                    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 class Incoming 
            { 
                public class Class 
                { 
                    static string name = null
                    public static string Name 
                    { 
                        get { return name; } 
                        set { name = value; } 
                    } 
                } 
     
                public class Time 
                { 
                    static string format = null
                    static int? sindex; 
                    static int? len; 
     
                    public static string Format 
                    { 
                        get { return format; } 
                        set { format = value; } 
                    } 
     
                    public static int? StartIndex 
                    { 
                        get { return sindex; } 
                        set { sindex = value; } 
                    } 
                    public static int? Length 
                    { 
                        get { return len; } 
                        set { len = value; } 
                    } 
                } 
     
                public class Date 
                { 
                    static string format = null
                    static int? sindex; 
                    static int? len; 
     
                    public static string Format 
                    { 
                        get { return format; } 
                        set { format = value; } 
                    } 
     
                    public static int? StartIndex 
                    { 
                        get { return sindex; } 
                        set { sindex = value; } 
                    } 
                    public static int? Length 
                    { 
                        get { return len; } 
                        set { len = value; } 
                    } 
                } 
            
                public class Duration 
                { 
                    static string format = null
                    static int? sindex; 
                    static int? len; 
     
                    public static string Format 
                    { 
                        get { return format; } 
                        set { format = value; } 
                    } 
     
                    public static int? StartIndex 
                    { 
                        get { return sindex; } 
                        set { sindex = value; } 
                    } 
                    public static int? Length 
                    { 
                        get { return len; } 
                        set { len = value; } 
                    } 
                } 
     
                public class Extension 
                { 
                    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 class DialNoCLID 
                { 
                    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; } 
                    } 
                } 
            
            } 
     
     
        } 

    Please help me.

    Best Regard
    Naythu
    Friday, October 3, 2008 3:11 AM

Answers

  • Hello,

            If you want to serialize an instance containing static fields of data, I think it is truly feasible.   Please refer to the following codes, and here you could use both BinaryFormatter and SoapFormatter.    However, we should let the static member class implement ISerializable, and then add GetObjectData and another contructor functions here.   Then, when we call serialize, it actually calls GetDataObject here, and when we call deserialize, it will call the corresponding constructor here.   Besides, I think serialize static member class is not a good design.  :-)


    --------------------------------------------------------------------------------------------------------------------------------------------------------------

            

        [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");

            }

        }



        public
    partial class Form1 : Form

        {

            MyClass myClass = null;

     

            public Form1()

            {

                InitializeComponent();

               

                MyClass.StartIndex = 1;

                MyClass.Length = 10;

                myClass = new MyClass();

            }

     

            private void buttonBinarySerialize_Click(object sender, EventArgs e)

            {

                FileStream fileStream = new FileStream("test.binary", FileMode.Create);

                BinaryFormatter bf = new BinaryFormatter();

                bf.Serialize(fileStream, myClass);

                fileStream.Dispose();

                fileStream.Close();

            }

     

            private void buttonBinaryDeserialize_Click(object sender, EventArgs e)

            {

                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 buttonSoapSerialize_Click(object sender, EventArgs e)

            {

                FileStream fileStream = new FileStream("test.soap", FileMode.Create);

                SoapFormatter sf = new SoapFormatter();

                sf.Serialize(fileStream, myClass);

                fileStream.Dispose();

                fileStream.Close();

            }

     

            private void buttonSoapDeserialize_Click(object sender, EventArgs e)

            {

                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());

            }

        }

    -------------------------------------------------------------------------------------
     

            Thanks,

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706 http://forums.msdn.microsoft.com/en-US/vssetup/thread/60424309-bd78-4ca2-b618-03c4a16123b6
    Tuesday, October 7, 2008 6:11 AM
    Moderator

All replies

  • I would really like to help you,but could you rephrase the question? I don't understand it.
    "I love deadlines. I like the whooshing sound they make as they fly by." - Douglas Adams
    Friday, October 3, 2008 3:25 AM
  • Sorry for my incomplete question.
    I'd like to know that
    how can I serialize an instance of a class which contains static member data- code is like the above question?
    I'd like to store static data in the serialized file.

    Regard
    Monday, October 6, 2008 1:48 AM
  • Hello,

            If you want to serialize an instance containing static fields of data, I think it is truly feasible.   Please refer to the following codes, and here you could use both BinaryFormatter and SoapFormatter.    However, we should let the static member class implement ISerializable, and then add GetObjectData and another contructor functions here.   Then, when we call serialize, it actually calls GetDataObject here, and when we call deserialize, it will call the corresponding constructor here.   Besides, I think serialize static member class is not a good design.  :-)


    --------------------------------------------------------------------------------------------------------------------------------------------------------------

            

        [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");

            }

        }



        public
    partial class Form1 : Form

        {

            MyClass myClass = null;

     

            public Form1()

            {

                InitializeComponent();

               

                MyClass.StartIndex = 1;

                MyClass.Length = 10;

                myClass = new MyClass();

            }

     

            private void buttonBinarySerialize_Click(object sender, EventArgs e)

            {

                FileStream fileStream = new FileStream("test.binary", FileMode.Create);

                BinaryFormatter bf = new BinaryFormatter();

                bf.Serialize(fileStream, myClass);

                fileStream.Dispose();

                fileStream.Close();

            }

     

            private void buttonBinaryDeserialize_Click(object sender, EventArgs e)

            {

                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 buttonSoapSerialize_Click(object sender, EventArgs e)

            {

                FileStream fileStream = new FileStream("test.soap", FileMode.Create);

                SoapFormatter sf = new SoapFormatter();

                sf.Serialize(fileStream, myClass);

                fileStream.Dispose();

                fileStream.Close();

            }

     

            private void buttonSoapDeserialize_Click(object sender, EventArgs e)

            {

                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());

            }

        }

    -------------------------------------------------------------------------------------
     

            Thanks,

    Best Regards,
    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706 http://forums.msdn.microsoft.com/en-US/vssetup/thread/60424309-bd78-4ca2-b618-03c4a16123b6
    Tuesday, October 7, 2008 6:11 AM
    Moderator