繁体   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