zoukankan      html  css  js  c++  java
  • C#复杂类型序列化

    [Serializable]
    public class CardItemInfo
    {
        private int lineWidth;//线宽
        private CardItemInfo childCardItemInfo;
        public int LineWidth
        {
            get { return lineWidth; }
            set
            {
                lineWidth = value;
            }
        }
        public int ChildCardItemInfo
        {
            get { return childCardItemInfo; }
            set
            {
                childCardItemInfo = value;
            }
        }
    }

    定义列表

    private static List<CardItemInfo> listItemInfo=new List<CardItemInfo>();

    序列化到XML

    public static bool SaveXml()
            {
                CardItemInfo cardItemInfo=new CardItemInfo();
                cardItemInfo.LineWidth = 11;
                CardItemInfo childCardItemInfo=new CardItemInfo();
                childCardItemInfo.LineWidth = 22;
                cardItemInfo.ChildCardItemInfo = childCardItemInfo;
                listItemInfo.Add(cardItemInfo);
                Stream fStream = new FileStream(System.Windows.Forms.Application.StartupPath + @"	est11.xml", FileMode.Create);
                XmlSerializer xmlFormat = new XmlSerializer(typeof(List<CardItemInfo>));
                xmlFormat.Serialize(fStream, listItemInfo);//序列化对象
                return false;
            }

    从XML反序列化

    public static bool LoadFromXml(String fileName)
            {
                try
                { 
                    System.IO.Stream fStream = new FileStream(fileName, FileMode.Open);
                    XmlSerializer xmlFormat = new XmlSerializer(typeof(List<CardItemInfo>));
                    listItemInfo = (List<CardItemInfo>)xmlFormat.Deserialize(fStream);
                    fStream.Close();
                    MessageBox.Show("OK");
     
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                    return false;
                }
                return true;
            }

     博客资源:QQ群616945527,博客相关资源,同名文件。

  • 相关阅读:
    以《淘宝网》为例,描绘质量属性的六个常见属性场景
    架构漫谈读后感
    软件体系架构课下作业01
    架构之美阅读笔记06
    架构之美阅读笔记05
    架构之美阅读笔记04
    架构之美阅读笔记03
    架构之美阅读笔记02
    架构之美阅读笔记01
    学习进度15
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/8526494.html
Copyright © 2011-2022 走看看