一个树形结构的序列化/ Derialization [英] Serialization / Derialization of a tree structure

查看:514
本文介绍了一个树形结构的序列化/ Derialization的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出最佳的方式来保存(序列化),后来打开(反序列化)的树形结构。我的结构是由各种对象类型具有不同的特性,但是从基抽象的节点类中的每个继承

I'm trying to figure out the best way to save (serialize) and later open (deserialize) a tree structure. My structure is made up of various object types with different properties, but each inherits from a base abstract "Node" class.

每个节点具有唯一的ID(GUID)和具有将设置一个节点的父节点的AddSuperNode(点ND)方法。这反过来又调用其他方法,让父节点知道有什么样的子节点。然而,一些节点也利用 AddAuxSuperNode()方式,增加了一个二级父节点。

Each node has unique ID (GUID), and has an AddSuperNode(Node nd) method that will set the parent of a node. This in turn calls other methods that allow the parent node to know what sub nodes it has. However, some nodes also utilize a AddAuxSuperNode() method that adds a secondary parent to the Node.

我是用二进制序列化,但现在我想我要使用的东西在那里我有更多的控制,以及序列化的数据更容易获得。我也想保留类型信息,当我反序列化,并且能够序列化私有值。因此,的DataContractSerializer 好像去的最佳途径。

I was using binary serialization, but now I think I want to use something where I have a bit more control, and the serialized data is more accessible. I also want to retain Type information when I deserialize, and be able to serialize private values. So DataContractSerializer seemed like the best way to go.

我不能只序列化根的节点由于直接节点有多个父母。我不希望创建重复的对象。因此,它似乎是我需要树解构成扁平列表,然后序列化。然后序列化该列表后重建树。这听起来是正确的?

I can't just serialize the root Node directly because of nodes having multiple parents. I do not want to create duplicate objects. So it would seem that I need to deconstruct the tree into a flat list, and then serialize that. Then after serializing that list reconstruct the tree. Does this sound right?

就像我之前说的每个节点有一个唯一的GUID标识,但现在的节点参考他们的父母/子女直接不存储它们的ID。我可以更新的 AddSuperNode() AddAuxSuperNode()方法也更新父ID列表中除了直接引用被序列化。但对象被序列化时,我宁愿只更新/创建这个列表。所以我想创建一个 UpdateSuperNodeIDRefs()中,将串行化之前右称为节点法。

Like I said before each Node has a unique GUID identifier, but right now Nodes reference their parents/children directly and do not store their ids. I could update the AddSuperNode() and AddAuxSuperNode() methods to also update a list of parent ids to be serialized in addition to the direct references. But I'd rather only update/create this list when the object is being serialized. So i was thinking create an UpdateSuperNodeIDRefs() method in the node that would be called right before serialization.

以下是我什么规划这种结构的序列化和反序列化的事情。任何人都可以建议更好/清洁/更有效的方式做到这一点?

The following is what I'm planning to do for serialization and deserialization of this structure. Can anyone suggestion a better/cleaner/more efficient way to do this?

连载

1)提供的树结构的根节点

1) Provide the root node of the tree structure

2)的树结构分解成一个扁平的词典(GUID标识,点ND)其中 ID 第二

2) Break down tree structure into a flat Dictionary(Guid id,Node nd) where id is the guid of nd.

3)调用在 GUID < STRONG> UpdateSuperNodeIDRefs();为每个节点更新它已保存其父母的ID。

3) Call UpdateSuperNodeIDRefs(); for each node to update the IDs it has saved for its parents.

4)序列化的词典节点是的DataContractSerializer

4) Serialize the Dictionary of nodes with DataContractSerializer

反序列化

1)反序列化的词典节点通过

1) Deserialize the Dictionary of nodes

2)Itterate每个节点词典后,重新连接到每一个他们的父母。对于存储任何父标识找到相应的节点(S)在词典相匹配的ID(S)调用在 AddSuperNode()或<强> AddAuxSuperNode()以重新connnect节点把它的父(S)

2) Itterate through each Node in the Dictionary, reconnecting each to their parents. For any Parent IDs stored find the respective Node(s) in the Dictionary with matching ID(s) call the AddSuperNode() or AddAuxSuperNode() to re-connnect the node to its parent(s)

3)从任何的节点在字典找到结构的根

3) From any Node in the Dictionary find the root of the structure

4)返回根的节点

推荐答案

如果一个节点有多个父母,那么它是不是一棵树;它是,据推测,一个的的。但是 - 不担心; 的DataContractSerializer 能处理这个要求:

If a node has multiple parents, then it isn't a tree; it is, presumably, a graph. However - worry not; DataContractSerializer can handle this for you:

using System;
using System.IO;
using System.Runtime.Serialization;

[DataContract]
class Node {
    [DataMember]
    public Node AnotherNode { get; set; }
}

static class Program
{
    static void Main()
    {
        Node a = new Node(), b = new Node();
        // make it a cyclic graph, to prove reference-mode
        a.AnotherNode = b;
        b.AnotherNode = a;

        // the preserveObjectReferences argument is the interesting one here...
        DataContractSerializer dcs = new DataContractSerializer(
            typeof(Node), null, int.MaxValue, false, true, null);
        using (MemoryStream ms = new MemoryStream())
        {
            dcs.WriteObject(ms, a);
            ms.Position = 0;
            Node c = (Node) dcs.ReadObject(ms);
            // so .AnotherNode.Another node should be back to "c"
            Console.WriteLine(ReferenceEquals(c, c.AnotherNode.AnotherNode));
        }

    }
}

这篇关于一个树形结构的序列化/ Derialization的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆