2015-01-06 29 views
0

我有這樣一個類:C#XML序列化不包括父類的字段

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

當我嘗試序列化訣竅發生了:我不希望它父母的(按鈕)字段進行序列化,因爲這給我序列化錯誤。所以後來,我可以反序列化這個XML得到節點陣列的創建,當我讀到他們的領域。 我可以忽略父類的序列不知何故? 謝謝。

+0

是吧'System.Windows.Forms.Button'或'System.Windows.Controls.Button',或者只是'Doesnt.Matter.Button'? –

+0

是System.Windows.Controls.Button –

回答

0

我會用包容去代替。並序列化包含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; } 
} 

作爲一個側面說明,this post剷斷「爲什麼不應該我用XmlSerializer仿製藥」。