如何序列化n深度ICollection构造

时间:2015-05-01 18:46:18

标签: c# xml entity-framework serialization entity-framework-6

想象一下,如果愿意,可以使用如下所示的数据结构:

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

这是作为一个例子严格提供的,与我的目标结构没什么关系,但它用于使用我们都应该熟悉的比喻来说明问题。是的,我知道,支持商店。

使用Entity Framework(特别是EF6)将结构设置为持久化。正如您所看到的,结构的深度是任意的,因为FolderThing可以包含其自身的倍数, ad infinitum 。我还希望能够将单个FolderThing及其所有子项导出为XML文件,如下所示:

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

前一段时间I asked how to serialize an ICollection,这个答案让我使用了DataContractSerializer。然后我问this question,并收到了一个有用的答案,只让我了解了我在这里提出的问题。那么,如何完全序列化大量使用ICollections来创建任意深度对象的数据结构呢?

0 个答案:

没有答案