C#XML序列化不包括父类字段
我有一门课是这样的:C#XML序列化不包括父类字段,c#,xml,wpf,serialization,C#,Xml,Wpf,Serialization,我有一门课是这样的: public abstract class Node : Button { [XmlIgnoreAttribute()] private bool isMovable; public abstract ObjectType Type { get; } public double X { get; set; } public dou
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);
ObjectXMLSerializer.Save(main.current_data.Nodes,文件名);
当我尝试序列化它时,技巧就发生了:我不希望它的父(按钮)字段被序列化,因为这会给我序列化错误。因此,稍后,我可以反序列化此xml,以获取在读取节点的字段时创建的节点数组。
我可以忽略父类的序列化吗?
谢谢。我会选择遏制。并序列化包含的
节点信息
。节点信息将是与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
”一起使用。是System.Windows.Forms.Button
还是System.Windows.Controls.Button
,还是仅仅Doesn.Matter.Button
?是System.Windows.Controls.Button