zoukankan      html  css  js  c++  java
  • 序列化反序列化对象XML文件写入Sample[简单但是有代表性]

    using System;

    namespace XMLTextWriter
    {
            
    /// <summary>
            
    /// The class for <see cref="EmployeeData"/> that 
            
    /// represents a record in the <c>Employee</c> table.
            
    /// </summary>

            public class EmployeeData
            
    {
                
    Private Fields

                
    Constructor

                
    Public Properties
            }



    }


    以上是一个Employee对象类:

    然后是基于Console程序的文件系统创建和流文件写入的Assistant类:

    using System;
    using System.Xml;
    using System.Text;
    using System.IO;
    using System.Xml.Serialization;


    namespace XMLTextWriter
    {
        
    /// <summary>
        
    /// XmlWriterAssitant 的摘要说明。
        
    /// </summary>

        public class XmlWriterAssitant
        
    {
            
    public XmlWriterAssitant()
            
    {
                
    //
                
    // TODO: 在此处添加构造函数逻辑
                
    //
            }

            
    /// <summary>
            
    /// 写入日志
            
    /// </summary>
            
    /// <param name="xmlFilePath">//@"C:\LogInfo\Log.xml"</param>

            public void WriterXML(string xmlFilePath)
            
    {
                
    //创建文件
                CreateFile(xmlFilePath);

                
    /**************/
                System.Xml.XmlTextWriter xmlWr
    =new XmlTextWriter(xmlFilePath,Encoding.ASCII);            

                xmlWr.Formatting 
    =Formatting.Indented;

                xmlWr.WriteStartDocument();

                xmlWr.WriteStartElement(
    "profile");
                xmlWr.WriteAttributeString(
    "company","usst");
                xmlWr.WriteStartElement(
    "employee");
                xmlWr.WriteAttributeString(
    "name","worker1");
                xmlWr.WriteAttributeString(
    "age","21");
                xmlWr.WriteStartElement(
    "job");
                xmlWr.WriteAttributeString(
    "title","Team Leader");

                xmlWr.WriteEndElement();

                xmlWr.WriteEndElement();

                xmlWr.WriteEndElement();


                xmlWr.WriteEndDocument();
                

                xmlWr.Flush();

                xmlWr.Close();

                Console.WriteLine(
    "XML写入完成");


                
    /*******************/

                CreateFile(
    @"C:\LogInfo\EmployeeInfo.xml");

                EmployeeData emp
    =new EmployeeData();
                emp.Address
    ="ShangHai";
                emp.Code
    ="001";
                emp.DeptID
    =1;
                emp.EmployeeID
    =1;
                emp.Name
    ="John Avent";
                emp.IsDelete
    =1;
                emp.PassWord
    ="001";
                emp.Phone
    ="021-12345678";
                emp.UserName
    ="User";
                emp.IsLogin
    =true;

                System.IO.StreamWriter sWriter
    =new StreamWriter(@"C:\LogInfo\EmployeeInfo.xml");
                
    //串行化对象
                System.Xml.Serialization.XmlSerializer xmlSer=new XmlSerializer(emp.GetType());

                xmlSer.Serialize(sWriter,emp);

                sWriter.Close();

                
    /******Read****/

                DeserializeStream(
    @"C:\LogInfo\EmployeeInfo.xml");

                
    /**************/

            }

            
    /// <summary>
            
    /// 反序列化一个对象
            
    /// </summary>
            
    /// <param name="xmlFilePath"></param>

            private void DeserializeStream(string xmlFilePath)
            
    {
                EmployeeData emp
    =new EmployeeData();

                System.IO.FileStream fStream
    =new FileStream(xmlFilePath,FileMode.Open,FileAccess.Read);

                System.Xml.Serialization.XmlSerializer xmlSer
    =new XmlSerializer(emp.GetType());
                
    //对读出的XML文件反序列化对象 
                emp=(EmployeeData)xmlSer.Deserialize(fStream);

                Console.WriteLine(
    "Employee's name is :"+emp.Name);
                Console.WriteLine(
    "Employee's phone is:"+emp.Phone);
                Console.WriteLine(
    "Employee's address is:"+emp.Address);

                Console.WriteLine(
    "That's all.");

                fStream.Close();            

            }


            
    private void CreateFile(string xmlFilePath)
            
    {
                
    try
                
    {
                    System.IO.DirectoryInfo dirOne
    =new DirectoryInfo(@"C:\LogInfo");
                    
    //Directory doesn't exits ,then we create the instance of the directory
                    if(!dirOne.Exists)
                    
    {
                        dirOne.Create();
                        dirOne.Refresh();
                        Console.WriteLine(
    "Directory 'C:LogInfo' Create Successd!");
                    }

                    
    else
                    
    {
                        
    //throw new Exception("Cann't Create the Directory!The Directory already exists!");
                    }

                    System.IO.FileInfo fileOne
    =new FileInfo(xmlFilePath);

                    
    //File Doesn't exists
                    if(!fileOne.Exists)
                    
    {
                        fileOne.Create();
                        fileOne.Refresh();
                        Console.WriteLine(
    "File {0} Create Successd!",xmlFilePath);
                    }

                    
    else
                    
    {
                        
    //throw new Exception("Cann't Create the file!The file already exists!");
                    }

                }

                
    catch
                
    {

                }

            }

        }

    }


    然后我们在主输入类调用这部分程序:
    using System;
    using System.Xml;

    using System.Text;
    using System.IO;

    namespace XMLTextWriter
    {
        
    /// <summary>
        
    /// MainClass 的摘要说明。
        
    /// </summary>

        class MainClass
        
    {
            
    /// <summary>
            
    /// 应用程序的主入口点。
            
    /// </summary>

            [STAThread]
            
    static void Main(string[] args)
            
    {
                
    //
                
    // TODO: 在此处添加代码以启动应用程序
                
    //        
                XmlWriterAssitant xwA=new XmlWriterAssitant();
                xwA.WriterXML(
    @"C:\LogInfo\Log.xml");


                

                
    /*88*******************/  
                Console.ReadLine();
            }

            
        }

    }


    本文来自博客园,作者:Slashout,转载请注明原文链接:https://www.cnblogs.com/SlashOut/archive/2005/04/19/140799.html 关注公众号:数字化转型

  • 相关阅读:
    软阴影的实现(转帖)
    卡巴斯基:警惕IE拦截器恶意推广导航网站 狼人:
    安全问题拷问着电子支付第三方未来 狼人:
    微软警告:泄露的Office 2010预览版或含病毒 狼人:
    《越狱》完结 米帅迷应小心纹身网站挂马 狼人:
    赛门铁克和McAfee:目标锁定iPhone! 狼人:
    McAfee将与EMC合作推出在线PC备份服务 狼人:
    恶意软件分析师:面临社交网络威胁的用户已10亿 狼人:
    Mac OS X现漏洞 苹果称是Java导致恶意攻击 狼人:
    暴风影音声明:DNS服务器才是故障源头 狼人:
  • 原文地址:https://www.cnblogs.com/SlashOut/p/140799.html
Copyright © 2011-2022 走看看