序列化反序列化对象XML文件写入Sample[简单但是有代表性]

None.gif using  System;
None.gif
None.gif
namespace  XMLTextWriter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// The class for <see cref="EmployeeData"/> that 
InBlock.gif        
/// represents a record in the <c>Employee</c> table.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public class EmployeeData
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif            
Private Fields#region Private Fields
InBlock.gif            
private int _employeeID;
InBlock.gif            
private short _deptID;
InBlock.gif            
private short _jobID;
InBlock.gif            
private string _code;
InBlock.gif            
private string _userName;
InBlock.gif            
private string _name;
InBlock.gif            
private string _passWord;
InBlock.gif            
private string _address;
InBlock.gif            
private string _phone;
InBlock.gif            
private short _isDelete;
InBlock.gif            
private bool _isLogin;
ExpandedSubBlockEnd.gif            
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif            
Constructor#region Constructor
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// Initializes a new instance of the <see cref="EmployeeData"/> class.
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            public EmployeeData ()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this._employeeID = 0;
InBlock.gif                
this._deptID = 0;
InBlock.gif                
this._jobID = 0;
InBlock.gif                
this._code = "";
InBlock.gif                
this._userName = "";
InBlock.gif                
this._name = "";
InBlock.gif                
this._passWord = "";
InBlock.gif                
this._address = "";
InBlock.gif                
this._phone = "";
InBlock.gif                
this._isDelete = 0;
InBlock.gif                
this._isLogin = false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif            
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif            
Public Properties#region Public Properties
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// Gets or sets the <c>EmployeeID</c> column value.
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <value>The <c>EmployeeID</c> column value.</value>

InBlock.gif            public int EmployeeID
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn this._employeeID; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gifthis._employeeID = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// Gets or sets the <c>DeptID</c> column value.
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <value>The <c>DeptID</c> column value.</value>

InBlock.gif            public short DeptID
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn this._deptID; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gifthis._deptID = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// Gets or sets the <c>JobID</c> column value.
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <value>The <c>JobID</c> column value.</value>

InBlock.gif            public short JobID
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn this._jobID; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gifthis._jobID = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// Gets or sets the <c>Code</c> column value.
InBlock.gif            
/// This column is nullable.
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <value>The <c>Code</c> column value.</value>

InBlock.gif            public string Code
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn this._code; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gifthis._code = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// Gets or sets the <c>UserName</c> column value.
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <value>The <c>UserName</c> column value.</value>

InBlock.gif            public string UserName
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn this._userName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gifthis._userName = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// Gets or sets the <c>Name</c> column value.
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <value>The <c>Name</c> column value.</value>

InBlock.gif            public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn this._name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gifthis._name = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// Gets or sets the <c>PassWord</c> column value.
InBlock.gif            
/// This column is nullable.
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <value>The <c>PassWord</c> column value.</value>

InBlock.gif            public string PassWord
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn this._passWord; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gifthis._passWord = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// Gets or sets the <c>Address</c> column value.
InBlock.gif            
/// This column is nullable.
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <value>The <c>Address</c> column value.</value>

InBlock.gif            public string Address
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn this._address; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gifthis._address = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// Gets or sets the <c>Phone</c> column value.
InBlock.gif            
/// This column is nullable.
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <value>The <c>Phone</c> column value.</value>

InBlock.gif            public string Phone
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn this._phone; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gifthis._phone = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// Gets or sets the <c>IsDelete</c> column value.
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <value>The <c>IsDelete</c> column value.</value>

InBlock.gif            public short IsDelete
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn this._isDelete; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gifthis._isDelete = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// Gets or sets the <c>IsLogin</c> column value.
InBlock.gif            
/// </summary>
ExpandedSubBlockEnd.gif            
/// <value>The <c>IsLogin</c> column value.</value>

InBlock.gif            public bool IsLogin
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn this._isLogin; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gifthis._isLogin = value; }
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif            
#endregion

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif

以上是一个Employee对象类:

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

None.gif using  System;
None.gif
using  System.Xml;
None.gif
using  System.Text;
None.gif
using  System.IO;
None.gif
using  System.Xml.Serialization;
None.gif
None.gif
None.gif
namespace  XMLTextWriter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// XmlWriterAssitant 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class XmlWriterAssitant
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public XmlWriterAssitant()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 写入日志
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="xmlFilePath">//@"C:\LogInfo\Log.xml"</param>

InBlock.gif        public void WriterXML(string xmlFilePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//创建文件
InBlock.gif
            CreateFile(xmlFilePath);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//**************/
InBlock.gif            System.Xml.XmlTextWriter xmlWr
=new XmlTextWriter(xmlFilePath,Encoding.ASCII);            
InBlock.gif
InBlock.gif            xmlWr.Formatting 
=Formatting.Indented;
InBlock.gif
InBlock.gif            xmlWr.WriteStartDocument();
InBlock.gif
InBlock.gif            xmlWr.WriteStartElement(
"profile");
InBlock.gif            xmlWr.WriteAttributeString(
"company","usst");
InBlock.gif            xmlWr.WriteStartElement(
"employee");
InBlock.gif            xmlWr.WriteAttributeString(
"name","worker1");
InBlock.gif            xmlWr.WriteAttributeString(
"age","21");
InBlock.gif            xmlWr.WriteStartElement(
"job");
InBlock.gif            xmlWr.WriteAttributeString(
"title","Team Leader");
InBlock.gif
InBlock.gif            xmlWr.WriteEndElement();
InBlock.gif
InBlock.gif            xmlWr.WriteEndElement();
InBlock.gif
InBlock.gif            xmlWr.WriteEndElement();
InBlock.gif
InBlock.gif
InBlock.gif            xmlWr.WriteEndDocument();
InBlock.gif            
InBlock.gif
InBlock.gif            xmlWr.Flush();
InBlock.gif
InBlock.gif            xmlWr.Close();
InBlock.gif
InBlock.gif            Console.WriteLine(
"XML写入完成");
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//*******************/
InBlock.gif
InBlock.gif            CreateFile(
@"C:\LogInfo\EmployeeInfo.xml");
InBlock.gif
InBlock.gif            EmployeeData emp
=new EmployeeData();
InBlock.gif            emp.Address
="ShangHai";
InBlock.gif            emp.Code
="001";
InBlock.gif            emp.DeptID
=1;
InBlock.gif            emp.EmployeeID
=1;
InBlock.gif            emp.Name
="John Avent";
InBlock.gif            emp.IsDelete
=1;
InBlock.gif            emp.PassWord
="001";
InBlock.gif            emp.Phone
="021-12345678";
InBlock.gif            emp.UserName
="User";
InBlock.gif            emp.IsLogin
=true;
InBlock.gif
InBlock.gif            System.IO.StreamWriter sWriter
=new StreamWriter(@"C:\LogInfo\EmployeeInfo.xml");
InBlock.gif            
//串行化对象
InBlock.gif
            System.Xml.Serialization.XmlSerializer xmlSer=new XmlSerializer(emp.GetType());
InBlock.gif
InBlock.gif            xmlSer.Serialize(sWriter,emp);
InBlock.gif
InBlock.gif            sWriter.Close();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//******Read****/
InBlock.gif
InBlock.gif            DeserializeStream(
@"C:\LogInfo\EmployeeInfo.xml");
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//**************/
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 反序列化一个对象
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="xmlFilePath"></param>

InBlock.gif        private void DeserializeStream(string xmlFilePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            EmployeeData emp
=new EmployeeData();
InBlock.gif
InBlock.gif            System.IO.FileStream fStream
=new FileStream(xmlFilePath,FileMode.Open,FileAccess.Read);
InBlock.gif
InBlock.gif            System.Xml.Serialization.XmlSerializer xmlSer
=new XmlSerializer(emp.GetType());
InBlock.gif            
//对读出的XML文件反序列化对象 
InBlock.gif
            emp=(EmployeeData)xmlSer.Deserialize(fStream);
InBlock.gif
InBlock.gif            Console.WriteLine(
"Employee's name is :"+emp.Name);
InBlock.gif            Console.WriteLine(
"Employee's phone is:"+emp.Phone);
InBlock.gif            Console.WriteLine(
"Employee's address is:"+emp.Address);
InBlock.gif
InBlock.gif            Console.WriteLine(
"That's all.");
InBlock.gif
InBlock.gif            fStream.Close();            
InBlock.gif
ExpandedSubBlockEnd.gif        }

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

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//throw new Exception("Cann't Create the Directory!The Directory already exists!");
ExpandedSubBlockEnd.gif
                }

InBlock.gif                System.IO.FileInfo fileOne
=new FileInfo(xmlFilePath);
InBlock.gif
InBlock.gif                
//File Doesn't exists
InBlock.gif
                if(!fileOne.Exists)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    fileOne.Create();
InBlock.gif                    fileOne.Refresh();
InBlock.gif                    Console.WriteLine(
"File {0} Create Successd!",xmlFilePath);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//throw new Exception("Cann't Create the file!The file already exists!");
ExpandedSubBlockEnd.gif
                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

然后我们在主输入类调用这部分程序:
None.gif using  System;
None.gif
using  System.Xml;
None.gif
None.gif
using  System.Text;
None.gif
using  System.IO;
None.gif
None.gif
namespace  XMLTextWriter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// MainClass 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    class MainClass
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加代码以启动应用程序
InBlock.gif            
//        
InBlock.gif
            XmlWriterAssitant xwA=new XmlWriterAssitant();
InBlock.gif            xwA.WriterXML(
@"C:\LogInfo\Log.xml");
InBlock.gif
InBlock.gif
InBlock.gif            
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//*88*******************/  
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/SlashOut/archive/2005/04/19/140799.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值