C#序列化與還原序列化操作

來源:互聯網
上載者:User

標籤:c#   序列化與還原序列化   

[Serializable]
    public class BillPropertyConfig
    {
        public BillPropertyConfig()
        {
            this.PlayItemBackCol = Color.Red;
            this.PlayItemFontCol = Color.Blue;
            this.FontSize = new Font("宋體", 10);
            this.FontOriSize = new Font("宋體", 10);
            this.Show_start = true;
            this.Show_model = true;
            this.Show_set = true;
            this.Show_title = true;
            this.Show_in = true;
            this.Show_dur = true;
            this.Show_you = true;
            this.Show_gua = true;
            this.Show_gua2 = true;
            this.Show_tai = true;
            this.Show_ju = true;
            this.Show_dao = true;
            this.Show_signal = true;
            this.Show_type = true;
            this.Show_department = true;
            this.Show_mappingMode = true;
        }
        [XmlIgnore]
        public Color PlayItemBackCol { get; set; }
        public int SerializerPlayItemBackCol
        {
            get
            {
                return ColorTranslator.ToWin32(PlayItemBackCol);
            }
            set
            {
                PlayItemBackCol = ColorTranslator.FromWin32(value);
            }
        }
        [XmlIgnore]
        public Color PlayItemFontCol { get; set; }
        public int SerializerPlayItemFontCol
        {
            get
            {
                return ColorTranslator.ToWin32(PlayItemFontCol);
            }
            set
            {
                PlayItemFontCol = ColorTranslator.FromWin32(value);
            }
        }
        [XmlIgnore]
        public Font FontSize { get; set; }
        public myFont SerializerFontSize
        {
            get
            {
                myFont _fontSize = new myFont();


                _fontSize.Name = FontSize.Name;
                _fontSize.Size = FontSize.Size;
                _fontSize.Style = FontSize.Style;


                return _fontSize;


            }


            set
            {
                FontSize = new Font(value.Name, value.Size, value.Style);
            }
        }
        [XmlIgnore]
        public Font FontOriSize { get; set; }
        public myFont SerializerFontOriSize
        {
            get
            {
                myFont _fontOriSize = new myFont();


                _fontOriSize.Name = FontOriSize.Name;
                _fontOriSize.Size = FontOriSize.Size;
                _fontOriSize.Style = FontOriSize.Style;


                return _fontOriSize;


            }


            set
            {
                FontOriSize = new Font(value.Name, value.Size, value.Style);
            }
        }
        public bool Show_start { get; set; }
        public bool Show_model { get; set; }
        public bool Show_set { get; set; }
        public bool Show_title { get; set; }
        public bool Show_in { get; set; }
        public bool Show_dur { get; set; }
        public bool Show_you { get; set; }
        public bool Show_gua { get; set; }
        public bool Show_gua2 { get; set; }
        public bool Show_tai { get; set; }
        public bool Show_ju { get; set; }
        public bool Show_dao { get; set; }
        public bool Show_signal { get; set; }
        public bool Show_type { get; set; }
        public bool Show_department { get; set; }
        public bool Show_mappingMode { get; set; }


        public string ToXML()
        {
            return StaticMethod.Obj2Xml(this);
        }


        public BillPropertyConfig FromXML(string str)
        {
            BillPropertyConfig m = StaticMethod.Xml2Obj(str, this.GetType()) as BillPropertyConfig;
            if (m != null)
            {
                return m;
            }
            return new BillPropertyConfig();
        }
    }
    [Serializable]
    public class myFont
    {
        public myFont()
        {
        }
        public string Name;
        public FontStyle Style;//這個可以包括其他幾項。
        public float Size;

    }

 public static string Obj2Xml(object obj)
        {
            string xmlStr = "";
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    XmlSerializer xml = new XmlSerializer(obj.GetType());
                    xml.Serialize(ms, obj);
                    byte[] arr = ms.ToArray();
                    xmlStr = Encoding.UTF8.GetString(arr);
                }
            }
            catch { }
            return xmlStr;
        }


        public static object Xml2Obj(string str, Type t)
        {
            if (str == "")
                return null;
            object obj = null;
            try
            {
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(str)))
                {
                    //XmlSerializer xml = XmlSerializer.FromTypes(new[] { t }).FirstOrDefault(); ;
                    XmlSerializer xml = new XmlSerializer(t);
                    obj = xml.Deserialize(ms);
                }
            }
            catch { }
            return obj;
        }

C#序列化與還原序列化操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.