C#序列化与反序列化操作

来源:互联网 发布:php大转盘源码 编辑:程序博客网 时间:2024/06/08 00:21
[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;
        }

0 0
原创粉丝点击