Click here to Skip to main content
15,030,954 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:

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; }
Updated 17-Sep-12 2:06am

1 solution

I Found the solution and documented here.

Serializing Complex Data Containing XDocument[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900