Imagine, if you will, a data construct that looks like this:
public class FolderThing
{
public long ID { get; set; }
public virtual ICollection<FolderThing> folders { get; set; }
public virtual ICollection<FileThing> files { get; set; }
}
public class FileThing
{
public long ID { get; set; }
public string filename { get; set; }
public string filesize { get; set; }
}
This is strictly provided as an example, and has little to do with my target structure, but it serves to illustrate the problem using a metaphor we should all be familiar with. Yes, I know, backing stores.
The structure is set up to be persisted using Entity Framework (EF6, specifically). As you can see, the depth of the structure is arbitrary, in that a FolderThing
can contain multiples of itself, ad infinitum. I also want to be able to export a single FolderThing
and all its children as an XML file that looks something like this:
<folderthing id="123456">
<files>
<filething id="456" filename="foo" filesize="125987">
<filething id="457" filename="bar" filesize="125900">
<filething id="458" filename="foobar" filesize="126987">
</files>
<folders>
<folderthing id="123">
<folderthing id="124">
<files>
<filething id="466" filename="oof" filesize="125987">
</files>
</folderthing>
</folderthing>
</folders>
</folderthing>
Some time ago, I asked how to serialize an ICollection
, and that answer led me to use the DataContractSerializer
. Then I asked this question, and received a helpful answer that only ran me into the problem I'm presenting here. So, how do I fully serialize a data structure that uses ICollections
heavily to create an arbitrary-depth object?