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#region Private Fields
private int _employeeID;
private short _deptID;
private short _jobID;
private string _code;
private string _userName;
private string _name;
private string _passWord;
private string _address;
private string _phone;
private short _isDelete;
private bool _isLogin;
#endregion


Constructor#region Constructor

/**//// <summary>
/// Initializes a new instance of the <see cref="EmployeeData"/> class.
/// </summary>
public EmployeeData ()

{
this._employeeID = 0;
this._deptID = 0;
this._jobID = 0;
this._code = "";
this._userName = "";
this._name = "";
this._passWord = "";
this._address = "";
this._phone = "";
this._isDelete = 0;
this._isLogin = false;
}
#endregion


Public Properties#region Public Properties

/**//// <summary>
/// Gets or sets the <c>EmployeeID</c> column value.
/// </summary>
/// <value>The <c>EmployeeID</c> column value.</value>
public int EmployeeID

{

get
{ return this._employeeID; }

set
{ this._employeeID = value; }
}


/**//// <summary>
/// Gets or sets the <c>DeptID</c> column value.
/// </summary>
/// <value>The <c>DeptID</c> column value.</value>
public short DeptID

{

get
{ return this._deptID; }

set
{ this._deptID = value; }
}


/**//// <summary>
/// Gets or sets the <c>JobID</c> column value.
/// </summary>
/// <value>The <c>JobID</c> column value.</value>
public short JobID

{

get
{ return this._jobID; }

set
{ this._jobID = value; }
}


/**//// <summary>
/// Gets or sets the <c>Code</c> column value.
/// This column is nullable.
/// </summary>
/// <value>The <c>Code</c> column value.</value>
public string Code

{

get
{ return this._code; }

set
{ this._code = value; }
}


/**//// <summary>
/// Gets or sets the <c>UserName</c> column value.
/// </summary>
/// <value>The <c>UserName</c> column value.</value>
public string UserName

{

get
{ return this._userName; }

set
{ this._userName = value; }
}


/**//// <summary>
/// Gets or sets the <c>Name</c> column value.
/// </summary>
/// <value>The <c>Name</c> column value.</value>
public string Name

{

get
{ return this._name; }

set
{ this._name = value; }
}


/**//// <summary>
/// Gets or sets the <c>PassWord</c> column value.
/// This column is nullable.
/// </summary>
/// <value>The <c>PassWord</c> column value.</value>
public string PassWord

{

get
{ return this._passWord; }

set
{ this._passWord = value; }
}


/**//// <summary>
/// Gets or sets the <c>Address</c> column value.
/// This column is nullable.
/// </summary>
/// <value>The <c>Address</c> column value.</value>
public string Address

{

get
{ return this._address; }

set
{ this._address = value; }
}


/**//// <summary>
/// Gets or sets the <c>Phone</c> column value.
/// This column is nullable.
/// </summary>
/// <value>The <c>Phone</c> column value.</value>
public string Phone

{

get
{ return this._phone; }

set
{ this._phone = value; }
}


/**//// <summary>
/// Gets or sets the <c>IsDelete</c> column value.
/// </summary>
/// <value>The <c>IsDelete</c> column value.</value>
public short IsDelete

{

get
{ return this._isDelete; }

set
{ this._isDelete = value; }
}


/**//// <summary>
/// Gets or sets the <c>IsLogin</c> column value.
/// </summary>
/// <value>The <c>IsLogin</c> column value.</value>
public bool IsLogin

{

get
{ return this._isLogin; }

set
{ this._isLogin = value; }
}
#endregion
}


}
以上是一个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();
}
}
}