Xml仅序列化基对象

本文关键字:对象 序列化 Xml | 更新日期: 2023-09-27 18:32:41

目前我正在尝试编写一组两个类:一个(BackgroundManagerSettings),它将保存我希望在程序关闭时保存的所有变量,另一个(BackgroundManager)包含仅在运行时需要的方法和变量。

法典:

public class BackgroundManager : BackgroundManagerSettings
    {
    //Example
    private string _LastName;
    public string LastName
    {
        get
        {
            return this._LastName;
        }
        set
        {
            this._LastName = value;
        }
    }
    public BackgroundManager()
    {
        this.LastName = "Smith";
    }
    public static BackgroundManager Load(string filename)
    {
        XmlSerializer Serializer = new XmlSerializer(typeof(BackgroundManager));
        LoopAgain:
        try
        {
            using (StreamReader Reader = new StreamReader(filename))
            {
                return Serializer.Deserialize(Reader) as BackgroundManager;
            }
        }
        catch (FileNotFoundException)
        {
            using (StreamWriter Writer = new StreamWriter(filename))
            {
                Serializer.Serialize(Writer, new BackgroundManager() as BackgroundManagerSettings);
                Writer.Close();
            }
            goto LoopAgain;
        }
        catch (InvalidOperationException)
        {
            File.Delete(filename);
            goto LoopAgain;
        }
    }
    public void Save(string filename)
    {
        XmlSerializer Serializer = new XmlSerializer(typeof(BackgroundManager));
        using (StreamWriter Writer = new StreamWriter(filename))
        {
            Serializer.Serialize(Writer, this as BackgroundManagerSettings);
            Writer.Close();
        }
    }
}
public abstract class BackgroundManagerSettings
{
    //Example
    private string _FirstName;
    [XmlElement("FirstName")]
    public string FirstName
    {
        get
        {
            return this._FirstName;
        }
        set
        {
            this._FirstName = value;
        }
    }
    public BackgroundManagerSettings()
    {
        this.FirstName = "Joe";
    }
}

用法:

BackgroundManager Manager = BackgroundManager.Load("temp.Xml");
Manager.Save("temp.Xml");

输出:

<?xml version="1.0" encoding="UTF-8"?>
<BackgroundManager xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <FirstName>Joe</FirstName>
     <LastName>Smith</LastName>
</BackgroundManager>

我目前的问题是,当我使用 Save() 方法时,它会序列化 BackgroundManager 的字段以及 BackgroundManagerSettings 的字段。如何使其仅序列化背景管理器设置的字段?任何帮助或链接为我指明正确的方向将不胜感激。

Xml仅序列化基对象

使用 XmlIgnore 属性标记不希望序列化的基类的所有公共成员。

我会这样做:

[DataContract]
public class BackgroundManager : BackgroundManagerSettings
{
    [DataMember]
    public int PropertyToSerialize { get; set; }
}

将 DataMember 修饰添加到要包含的所有属性。

经过更多的研究,我找到了一种方法,可以按照我想要的方式工作,谢谢你的建议。

 public static BackgroundManager Load(string filename)
    {
        XmlSerializer Serializer = new XmlSerializer(typeof(BackgroundManager));
        LoopAgain:
        try
        {
            using (StreamReader Reader = new StreamReader(filename))
            {
                return Serializer.Deserialize(Reader) as BackgroundManager;
            }
        }
        catch (FileNotFoundException)
        {
            XmlSerializer BaseSerializer = new XmlSerializer(typeof(BackgroundManagerSettings));
            using (StreamWriter Writer = new StreamWriter(filename))
            {
                BaseSerializer.Serialize(Writer, new BackgroundManager().ToBase());
                Writer.Close();
            }
            goto LoopAgain;
        }
        catch (InvalidOperationException)
        {
            File.Delete(filename);
            goto LoopAgain;
        }
    }
    public void Save(string filename)
    {
        XmlSerializer Serializer = new XmlSerializer(typeof(BackgroundManagerSettings));
        using (StreamWriter Writer = new StreamWriter(filename))
        {
            Serializer.Serialize(Writer, this.ToBase());
            Writer.Close();
        }
    }
    private dynamic ToBase()
    {
        var Temp = Activator.CreateInstance(typeof(BackgroundManagerSettings));
        FieldInfo[] Fields = Temp.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
        foreach (FieldInfo x in Fields)
        {
            x.SetValue(Temp, x.GetValue(this));
        }
        return Temp;
    }