Click here to Skip to main content
Sign Up to vote bad
good
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 - 2:03
Edited 17 Sep '12 - 2:06


1 solution

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
Your Filters
Interested
Ignored
     
0 OriginalGriff 223
1 Ron Beyer 220
2 Aarti Meswania 200
3 Mahesh Bailwal 175
4 Rohan Leuva 170
0 Sergey Alexandrovich Kryukov 8,553
1 OriginalGriff 6,899
2 CPallini 3,648
3 Rohan Leuva 2,963
4 Maciej Los 2,308


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 19 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid