簡體   English   中英

C#XML序列化,不包括父類字段

[英]C# XML Serialization excluding parent class fields

我有這樣的課:

public abstract class Node : Button
    {
        [XmlIgnoreAttribute()]
        private bool isMovable;

        public abstract ObjectType Type
        {
            get;
        }
        public double X { get; set; }
        public double Y { get; set; }
        public string Nodename { get; set; }
    }

序列化過程:

ObjectXMLSerializer<List<Node>>.Save(main.current_data.Nodes, filename);

當我嘗試對其進行序列化時,就會發生這種竅門:我不想序列化其父級的(Button)字段,因為這給了我序列化錯誤。 因此,稍后,我可以反序列化此xml以獲得在讀取它們具有的字段時創建的Node數組。 我可以以某種方式忽略父類的序列化嗎? 謝謝。

我會選擇圍堵。 並序列化包含的NodeInfo 節點信息與wpf按鈕的特定區別是您要序列化的其他信息。

public class ButtonNode : System.Windows.Controls.Button
{
    private System.Windows.Controls.Button _button;
    public ButtonNode(System.Windows.Controls.Button btn) : base() { this._button = btn; }

    public NodeInfo NodeInfo { get; set; }
}


public interface INodeInfo { ObjectType Type { get; } }

[XmlInclude(typeof(ConcreteNodeInfo1))]
public abstract class NodeInfo : INodeInfo
{
    public NodeInfo() { }

    [XmlIgnore] private bool isMovable;
    public abstract ObjectType Type { get; }
    public double X { get; set; }
    public double Y { get; set; }
    public string NodeName { get; set; }
}

public class ConcreteNodeInfo1 : NodeInfo 
{
    public ConcreteNodeInfo1() : base () { }
    public override ObjectType Type { get { return ObjectType.ObjectType1; }
}

附帶說明一下, 這篇文章解決了“為什么我不應該在XmlSerializer使用泛型”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM