xml序列化复杂些对象

来源:互联网 发布:mac双色修容粉多少钱 编辑:程序博客网 时间:2024/04/28 15:51

 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()  );
     }
 }

原创粉丝点击