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

xml序列化复杂的对象

Posted on 2007-04-27 13:19  james.dong  阅读(312)  评论(0编辑  收藏  举报
[Serializable]    
public abstract a
{
     private string _name = string.empty;
    [XmlAttribute()]
     public string Name
      {
           get{ return _name;}
           set{ _name = value;}
      }
}
[Serializable]
 public class AA:a
{
    .......
}
 [Serializable]
   public class BB :a
{
      ........
}
[Serializable]
 public class XX
{
     private  List<a> _children = new List<a>();
     [System.Xml.Serialization.XmlArray("as")]
     [System.Xml.Serialization.XmlArrayItem("AA", typeof(AA)),
        System.Xml.Serialization.XmlArrayItem("BB", typeof(BB))]
     public List<a> Children
    {
           get{ return _children;}
           set{_children = value;}
    }
}

 public Main()
{
       XX x = new  XX();
       x.Children.add( new AA());
       x.Children.add( new BB());
       XmlSerializer s = new  XmlSerializer( x.gettype() );
       using ( stringWrite w = new stringwriter() )
     {
            w.Serialize( w ,  xx.gettype()  );
     }
 }