博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

xml Serialization

Posted on 2006-07-29 20:20  james.dong  阅读(186)  评论(0编辑  收藏  举报


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

namespace xml_serialization
{
 /// <summary>
 /// Employ 的摘要说明。
 /// </summary>
 
 [XmlRoot(ElementName="Employ")]
 public class Employ
 {
  public Employ()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  
  private string _empId = string.Empty;
  [XmlAttribute( AttributeName="EmpID")]
  public string EmpId
  {
   get
   {
    return _empId;
   }
   set
   {
    _empId = value;
   }
  }

  private string _name = string.Empty;

  [XmlElement(ElementName="Name")]
  public string Name
  {
   get
   {
    return _name;
   }
   set
   {
    _name = value;
   }
  }
  private string _telephone = string.Empty;
  [XmlElement( ElementName="Telephone" )]
  public string TelePhone
  {
   get
   {
    return _telephone;
   }
   set
   {
    _telephone = value;
   }
  }
  private string _address = string.Empty ;
  [XmlElement( ElementName="Address")]
  public string Address
  {
   get
   {
    return _address;
   }
   set
   {
    _address = value;
   }
  }
  
 }

}

public class Test
{
    public Test()
{
}
Employ x = new Employ();
   x.EmpId = "0001";
   x.Name ="james.dong";
   x.TelePhone ="13750193476";
   x.Address ="打官司";

   XmlSerializer serial = new XmlSerializer( typeof( Employ ) );
   StreamWriter sw =File.CreateText( "E:\\1.xml" );
   serial.Serialize( sw, x );
   sw.Close();

   StreamReader fr = File.OpenText( "E:\\1.xml");
   XmlSerializer des = new XmlSerializer( typeof( Employ ) );
   Employ w = ( Employ)des.Deserialize( fr );
   fr.Close();
   
    System.Windows.Forms.Message.Show( w.EmpId + w.Name+w.Telephone+w.Address);
}