none
XML反序列化时,对于自关联的类型,微软的Deserialize如何做到以下几点的? RRS feed

  • 问题

  • static void Main(string[] args)
     {
      StringWriter writer = null;
      StringReader reader = null;
    //序列化嵌套数据结构
      TreeDataClass rootItem = new TreeDataClass();
      rootItem.Count = 1;
      rootItem.Key = "RootItem_Key";
    
      TreeDataClass childItem = new TreeDataClass();
      childItem.Count = 2;
      childItem.Key = "ChildItem_Key";
      childItem.Parent = rootItem;
    
      TreeDataClass childItem2 = new TreeDataClass();
      childItem2.Count = 2;
      childItem2.Key = "ChildItem_Key";
      childItem2.Parent = rootItem;
    
      List<TreeDataClass> lstItems = new List<TreeDataClass>() { childItem, childItem2 };
      writer = new StringWriter();
      XmlUtils.XmlSerialize<List<TreeDataClass>>(writer, lstItems);
      Console.WriteLine(writer.ToString());
      Console.ReadKey();
    
      reader = new StringReader(writer.ToString());
      List<TreeDataClass> lstItemsNew = XmlUtils.XmlDeserialize<List<TreeDataClass>>(reader);
    
    //微软的Deserialize是如何实现这两个实例引用的父类型是同一个实例?以下输出是True
      Console.WriteLine(object.ReferenceEquals(lstItems[0].Parent, lstItems[1].Parent));
      //Console.WriteLine(object.ReferenceEquals(lstItems[2].Parent, lstItems[1].Parent));
      Console.ReadKey();
    
      XmlSerializeWrapper<List<TreeDataClass>> wrapper3 = new XmlSerializeWrapper<List<TreeDataClass>>(lstItems);
      writer = new StringWriter();
      XmlUtils.XmlSerialize<XmlSerializeWrapper<List<TreeDataClass>>>(writer, wrapper3);
      Console.WriteLine(writer.ToString());
      Console.ReadKey();
    
      reader = new StringReader(writer.ToString());
      XmlSerializeWrapper<List<TreeDataClass>> newWrapper3 = null;
      newWrapper3 = (XmlSerializeWrapper<List<TreeDataClass>>)XmlUtils.XmlDeserialize<XmlSerializeWrapper<List<TreeDataClass>>>(reader);
    //这个上面的方法是我实现了ISerializable接口后自己根据xml内容读取出来后一个一个创建出来的对象(利用的都是反射的原理),但是这里输出的是false,也就是我根据不同段创建出了两个实例,显然不是希望得到的结果,这里应该如何控制?
      Console.WriteLine(object.ReferenceEquals(newWrapper3.SerializeObject[0].Parent, newWrapper3.SerializeObject[1].Parent));
      //Console.WriteLine("Key = {0}", newWrapper3.SerializeObject.Key);
      //Console.WriteLine("Count = {0}", newWrapper3.SerializeObject.Count);
    
      Console.ReadKey();
    }
    
    [Serializable]
     public class TreeDataClass
     {
     public TreeDataClass Parent { get; set; }
    
     public int Count { get; set; }
     public string Key { get; set; }
     }
    
    public class XmlUtils
     {
      public static void XmlSerialize<T>(XmlWriter writer, T serializeObject)
      {
       if (writer == null || serializeObject == null)
        throw new ArgumentNullException();
    
       XmlSerializer serializer = new XmlSerializer(serializeObject.GetType());
       serializer.Serialize(writer, serializeObject);
      }
    
      public static void XmlSerialize<T>(TextWriter writer, T serializeObject)
      {
       if (writer == null || serializeObject == null)
        throw new ArgumentNullException();
    
       XmlSerializer serializer = new XmlSerializer(serializeObject.GetType());
       serializer.Serialize(writer, serializeObject);
      }
    
      public static void XmlSerialize<T>(Stream stream, T serializeObject)
      {
       if (stream == null || serializeObject == null)
        throw new ArgumentNullException();
       XmlSerializer serializer = new XmlSerializer(serializeObject.GetType());
       serializer.Serialize(stream, serializeObject);
      }
    
      public static T XmlDeserialize<T>(XmlReader reader)
      {
       if (reader == null)
        throw new ArgumentNullException();
    
       XmlSerializer serializer = new XmlSerializer(typeof(T));
       return (T)serializer.Deserialize(reader);
      }
    
      public static T XmlDeserialize<T>(TextReader reader)
      {
       if (reader == null)
        throw new ArgumentNullException();
    
       XmlSerializer serializer = new XmlSerializer(typeof(T));
       return (T)serializer.Deserialize(reader);
      }
    
      public static T XmlDeserialize<T>(Stream stream)
      {
       if (stream == null)
        throw new ArgumentNullException();
       XmlSerializer serializer = new XmlSerializer(typeof(T));
       return (T)serializer.Deserialize(stream);
      }
     }

    针对上述这种自关联实现的树形数据类,我的问题如下:

    1、自定义序列化时,有什么办法控制那个Parent是嵌套的?

    2、另外自定义反序列化时,如何能够得到那两个Parent指向的是一个实例?

     

    本人自己写的这个序列化类库,就是为了突破微软的序列化方法不支持对接口、集合、派生类等进行序列化的限制。我的库目前已经 实现,但是解决不了上述存在的问题,所以过来请教专家。。。如果解决了,我把整个Sourece扔出去共享,我觉得应该有点意义,现在满世界都是问如何对接口、集合等受限制的类型进行序列化。。。我找不到答案与是我自己写了一个

     

    希望大家帮忙解决这个问题。

    • 已编辑 linus wang 2010年12月1日 12:57 增加源代码信息
    2010年12月1日 12:54