Click here to Skip to main content
12,818,964 members (29,376 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# serialization

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; }

Posted 17-Sep-12 3:03am
Updated 17-Sep-12 3:06am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 19 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100