默认是不行的
不过你可以让需要序列化的类实现ISerializable 接口,自行控制序列化和反序列化过程。

解决方案 »

  1.   

    我知到ISerializable有要重写GetData方法XmlSerializer再序列化时,也会调用它吗?
      

  2.   

    yes,
    you can do like this:
    [Serializable]
    class test
    {
    private string name;
    protected test(SerializationInfo info, StreamingContext context):
                                     base(info, context)
    {
    name = info.GetString("name");
    }
    public override void GetObjectData( SerializationInfo info, StreamingContext context ) 
    {
    info.AddValue("name", name, typeof(string));
    base.GetObjectData(info,context);
    }
    }
    }
      

  3.   

    D:\My Documents\Visual Studio Projects\test\Class1.cs(60): “test.dsds.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)” : 没有找到适合的方法来重写
      

  4.   

    是IXmlSerializable接口而不是ISerializable接口
    后者只能用于SoapFormatter和BinaryFormatter(这两个会序列化所有字段,和XmlSerializer不一样)这样可能实现IXmlSerializable接口即可
      

  5.   

    [Serializable]
    public class XmlSerializerTest : IXmlSerializable
    {
    public int i = 5;
    private int j = 6; public void Test()
    {
    Console.WriteLine(j);
    } #region IXmlSerializable 成员 public void WriteXml(XmlWriter writer)
    {
    // 添加 XmlSerializerTest.WriteXml 实现
    writer.WriteElementString("i",i.ToString());
    writer.WriteElementString("j",j.ToString());
    } public System.Xml.Schema.XmlSchema GetSchema()
    {
    // TODO:  添加 XmlSerializerTest.GetSchema 实现
    return null;
    } public void ReadXml(XmlReader reader)
    {
    // TODO:  添加 XmlSerializerTest.ReadXml 实现
    } #endregion
    }// test:
    // XmlSerializer xs = new XmlSerializer(typeof(XmlSerializerTest));
    // xs.Serialize(Console.Out,new XmlSerializerTest());
      

  6.   

    为此主题写了个blog:
    http://blog.sunmast.com/Sunmast/archive/2005/01/17/1168.aspx