[英]C# XML Serialization excluding parent class fields
I have a class like this: 我有这样的课:
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; }
}
Serialization process: 序列化过程:
ObjectXMLSerializer<List<Node>>.Save(main.current_data.Nodes, filename);
The trick happens when I try to serialize it: I don't want its parent's (Button) fields to be serialized, because this gives me serialization errors. 当我尝试对其进行序列化时,就会发生这种窍门:我不想序列化其父级的(Button)字段,因为这给了我序列化错误。 So later, I can deserialize this xml to get an array of Nodes created when I read the fields they have.
因此,稍后,我可以反序列化此xml以获得在读取它们具有的字段时创建的Node数组。 Can I ignore the serialization of the parent's class somehow?
我可以以某种方式忽略父类的序列化吗? Thanks.
谢谢。
I'd go with containment instead. 我会选择围堵。 And serialize the contained
NodeInfo
. 并序列化包含的
NodeInfo
。 Node information would be the specific difference from a wpf button, the additional info you want to serialize. 节点信息与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; }
}
As a side note, this post tackles the 'why shouldn't I use generics with XmlSerializer
'. 附带说明一下, 这篇文章解决了“为什么我不应该在
XmlSerializer
使用泛型”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.