码迷,mamicode.com
首页 > 其他好文 > 详细

xml序列化与反序列化

时间:2015-05-20 23:36:53      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

XmlConfig.cs
技术分享
namespace Model
{
    [ Serializable]
    public class XmlConfig
    {

        [ XmlElement]
        public List <Class1> Class1; 
        [ XmlElement]
        public List <Class2> Class2; 

        [ XmlIgnore]
        private static XmlConfig list;

        public static void SetModelList( XmlConfig zlist)
        {
            list = zlist;
        }

        public static XmlConfig GetModelList()
        {
            if (list == null )
            {
                list = new XmlConfig ();
            }
            return list;
        }
      

      
        public XmlConfig(){}
    }
    
    [ Serializable]
    public class Class2
    {
        [ XmlElement]
        public List <Class3> Class3;
        public Class2()
        {
        }
        [ XmlAttribute]
        public string Name;//名称
     
    }
   
    [ Serializable]
    public class Class3
    {
        publicClass3() { }
        [ XmlAttribute]
        
    }
}
View Code
XmlSerialiaze.cs
namespaceModel
{
  public   class XmlSerialiaze
    {
        //序列化
        public static void Serialiaze()
        {           
            XmlConfig p = XmlConfig .GetModelList();
Class1 tb
= new Class1(); Class1 temp = p.Class1[0]; tb.Name = temp.Name; p.Class1.Add(tb); //添加 tb.Class2= new List <Class2>(); Class2 cb = new Class2(); Class2 cool = temp.Class2[0]; tb.Class2.Add(cb);

XmlSerializer xs = new XmlSerializer( typeof(XmlConfig )); Stream stream = new FileStream( AppDomain.CurrentDomain.BaseDirectory + "XmlConfig.xml" , FileMode.Create, FileAccess .Write, FileShare.ReadWrite); xs.Serialize(stream, p); stream.Close(); } //反序列化 //XmlConfig p; public static void Deserialize() { XmlSerializer xs = new XmlSerializer( typeof(XmlConfig )); Stream stream = new FileStream( AppDomain.CurrentDomain.BaseDirectory + "XmlConfig.xml" , FileMode.Open, FileAccess .Read, FileShare.ReadWrite); XmlConfig p = (XmlConfig )xs.Deserialize(stream); XmlConfig.SetModelList(p); } } }

 

xml序列化与反序列化

标签:

原文地址:http://www.cnblogs.com/JoanLin-workNotes/p/4518344.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!