序列化包含XDocument属性的复杂对象图 [英] Serializing Complex Object Graph that contains XDocument property

查看:98
本文介绍了序列化包含XDocument属性的复杂对象图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个复杂的对象图,其中包含接口类型的属性,并且这些接口之一的实现使用XDocument支持的属性.我需要将此对象图序列化为XML,以获取重新水化的单元测试数据.首选XML,这样以后我就可以编辑数据了.

问题是我知道XmlSerializer无法处理接口,所以我将其定义为可能的接口实现的extraTypes移到了DataContractSerializer.它具有xml中列出的XDocument属性,但是没有值".反序列化时,缺少支持数据,因此操作这些值的代码失败.

下面是这种情况的示例.我没有对IFlexibleObject实现的完全控制,因此需要处理这种情况.

Hi,

I have a complex object graph that contains properties that are of an interface type and the implementation of one of those interfaces uses an XDocument backed property. I need to serialize this object graph to XML for rehydrated unit test data. XML is preferred so I can edit the data in the future.

Issue is that I know XmlSerializer cannot handle interfaces so I moved to DataContractSerializer with extraTypes defined for the possible interface implementations. It has the XDocument property listed in the xml however there is no ''value''. On deserialization The backing data is missing and so code manipulating these values fails.

Below is an example of the situation. I do not have full control over the IFlexibleObject implementations so need to handle this case.

public interface IFlexibleObject
{
    string BackingData { get; }
    void PlayWithValues();
}

public SomeFlexibleObject : IFlexibleObject
{
    private XDocument _doc = XDocument.Parse("<testing><help>23435</help></testing>");

    public BackingData { get { return _doc.ToString(); } }

    public void PlayWithValues()
    {
        // Do something with the help node value
    }
}

public ComplexObject
{
    public string SomeValue { get; set; }
    public IFlexibleObject FlexibleObject { get; set; }
}

推荐答案

我找到了解决方案,并在此处进行了说明.

序列化包含XDocument的复杂数据 [
I Found the solution and documented here.

Serializing Complex Data Containing XDocument[^]


这篇关于序列化包含XDocument属性的复杂对象图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆