none
請問如何用 XmlSerializer 儲存static 變數 RRS feed

  • 問題

  • 各位前輩好,

    因為有些環境設定或者使用者設定想儲存,

    上網找之後決定用XmlSerializer儲存成xml。

    目前我有個放設定的class叫做Class_Setting,所有關於設定的部分都存在Class_Setting,

    public class Class_Setting
    {
        static public int aaa = 220;
        public int bbb = 110;
    }

    儲存的程式碼如下

    XmlSerializer MyXmlSerializer = new XmlSerializer(typeof(Class_Setting));
    StreamWriter MyStreamWriter = new StreamWriter(Save_FilePath, false);
    
    Class_Setting Setting = new Class_Setting();
    MyXmlSerializer.Serialize(MyStreamWriter, Setting);

    儲存後的xml內容如下

    <?xml version="1.0" encoding="utf-8"?>
    <Class_Setting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <bbb>110</bbb>
    </Class_Setting>

    因為Class_Setting內的static變數都是程式要用的到設定,所以我宣告成全域,

    但是儲存後的xml中static沒存到,這讓我蠻苦惱的。

    所以有兩個問題想為各位前輩,

    1. 如何將static變數儲存成xml?

    2. 如何將Class_Setting內的aaa與bbb同時儲存成xml起來?

    2013年6月6日 上午 05:58

解答

  • namespace CSharp
    {
        public class Class_Setting : IXmlSerializable
        {
            public static int aaa = 220;
            public int bbb = 110;
     
            public System.Xml.Schema.XmlSchema GetSchema()
            {
                throw new NotImplementedException();
            }
     
            public void ReadXml(XmlReader reader)
            {
                throw new NotImplementedException();
            }
     
            public void WriteXml(XmlWriter writer)
            {
                writer.WriteRaw(Environment.NewLine);
                foreach (var item in this.GetType().GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static|System.Reflection.BindingFlags.Instance))
                {
                    writer.WriteRaw("<" + item.Name + ">" + item.GetValue(this).ToString() + "</" + item.Name + ">" + Environment.NewLine);
                }
            }
        }
     
        class Program
        {
            public int a = 1;
            public static int b = 1;
            static void Main(string[] args)
            {
                XmlSerializer ser = new XmlSerializer(typeof(Class_Setting));
                ser.Serialize(new FileStream("d:\\try.xml"FileMode.OpenOrCreate), new Class_Setting());
                Console.WriteLine("OK");
     
            }
        }
    }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    • 已標示為解答 笨嘎嘎 2013年6月6日 上午 06:58
    2013年6月6日 上午 06:47

所有回覆

  • XML 序列化是將物件的公用屬性和欄位轉換為一序列 的格式儲存或傳送的過程,此可參閱http://msdn.microsoft.com/zh-tw/library/system.xml.serialization.xmlserializer.aspx,因此本就不包含修飾詞Midifier,關於static的說明可參考http://msdn.microsoft.com/zh-tw/library/98f28cdx(v=vs.80).aspx
    2013年6月6日 上午 06:38
  • namespace CSharp
    {
        public class Class_Setting : IXmlSerializable
        {
            public static int aaa = 220;
            public int bbb = 110;
     
            public System.Xml.Schema.XmlSchema GetSchema()
            {
                throw new NotImplementedException();
            }
     
            public void ReadXml(XmlReader reader)
            {
                throw new NotImplementedException();
            }
     
            public void WriteXml(XmlWriter writer)
            {
                writer.WriteRaw(Environment.NewLine);
                foreach (var item in this.GetType().GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static|System.Reflection.BindingFlags.Instance))
                {
                    writer.WriteRaw("<" + item.Name + ">" + item.GetValue(this).ToString() + "</" + item.Name + ">" + Environment.NewLine);
                }
            }
        }
     
        class Program
        {
            public int a = 1;
            public static int b = 1;
            static void Main(string[] args)
            {
                XmlSerializer ser = new XmlSerializer(typeof(Class_Setting));
                ser.Serialize(new FileStream("d:\\try.xml"FileMode.OpenOrCreate), new Class_Setting());
                Console.WriteLine("OK");
     
            }
        }
    }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    • 已標示為解答 笨嘎嘎 2013年6月6日 上午 06:58
    2013年6月6日 上午 06:47
  • 謝謝Mark Su大大的回覆,

    你所提供的這兩個連結我之前看過,

    但是我心中的困惑還是無法得到解答~"~。

    我看過國外的網站,

    目前是有看到有人建議在宣告另外一個Class_Setting_New

    Class_Setting的靜態變數都放在Class_Setting_New環境變數中,

    public class Class_Setting
    {
        static public int aaa = 220;
    }
    
    public class Class_Setting_New
    {
        public int aaa;
    }
    

    接下來只要儲存Class_Setting_New就可以。

    但是這樣子多了一個步驟,

    所以希望問一下各位前輩有沒有更好的做法,

    或者各位前輩都如何儲存環境設定的變數?

    2013年6月6日 上午 06:50
  • 实现IXmlSerializable接口,自己使用反射完成(参考我的示例代码)。

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月6日 上午 06:55
  • 謝謝ProprammingVolunteer大大的解答,

    大大的範例程式一目瞭然,

    這樣子讀取我也會自己寫了。

    再次感謝各位有回答的大大。

    2013年6月6日 上午 07:02