Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# serialization
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; }
}
 
Posted 17-Sep-12 3:03am
Edited 17-Sep-12 3:06am
v2

1 solution

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

Solution 1

I Found the solution and documented here.
 
Serializing Complex Data Containing XDocument[^]
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 7,373
1 OriginalGriff 5,571
2 Peter Leow 4,097
3 CHill60 2,858
4 Mika Wendelius 2,850


Advertise | Privacy | Mobile
Web03 | 2.8.150224.1 | Last Updated 19 Sep 2012
Copyright © CodeProject, 1999-2015
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