seeXML Serialization in the .NET Framework
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml01202003.asp

解决方案 »

  1.   

    自定义类:
    [Serializable()]
    public class myclass:ISerializable
    {
    public int he;
    void ISerializable.GetObjectData(
    SerializationInfo info, StreamingContext context) 
    {
    // Instead of serializing this object, 
    // serialize a SingletonSerializationHelp instead.
    info.SetType(typeof(myclass));
    he =  info.GetInt32("he");
    // No other values need to be added.
    } }
    类输出为xml
    myclass mc = new myclass();
    System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer( typeof(myclass) );
    mc = (myclass)xs.Deserialize( xtr );
      

  2.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    myclass mc = new myclass();
    myclass mn = new myclass();
    mn.he =1000;
    mc.next = mn;
    mc.he = 100;
    mn.next = mc;
    System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer( typeof(myclass) );
    System.Xml.XmlTextWriter xtw = new System.Xml.XmlTextWriter( "1.xml" , System.Text.Encoding.Unicode);
    xs.Serialize( xtw , mc );
    }
    }
    [Serializable()]
    public class myclass:ISerializable
    {
    public myclass next;
    public int he;
    void ISerializable.GetObjectData(
    SerializationInfo info, StreamingContext context) 
    {
    // Instead of serializing this object, 
    // serialize a SingletonSerializationHelp instead.
    info.SetType(typeof(myclass));
    he =  info.GetInt32("he");
    // No other values need to be added.
    } }