序列化反序列化对象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
}
}
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.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();
}
}
}
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