.NET的XmlSerializer并多次引用同一个对象 [英] .NET XmlSerializer and multiple references to the same object

查看:183
本文介绍了.NET的XmlSerializer并多次引用同一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的仓库有名单,其中,学生> 名单,其中,课程> 名单,其中,报名> 凡报名有Enrolment.Student和Enrolment.Course这是引用两个previous列出了学生和课程之一。

My repository has List<Student>, List<Course> and List<Enrolment> where an Enrolment has Enrolment.Student and Enrolment.Course which are references one of the students or courses in the two previous lists.

当我使用的XmlSerializer在我的仓库它输出的冗余数据,将其序列中每个学生的所有属性列表&LT;学生&GT; 然后再为每个引用这些相同的学生名单,其中,报名&GT; 。我正在寻找一个优雅的方式来解决这个问题。

When I use XmlSerializer on my repository it outputs redundant data as it serializes all properties of each student in List<Student> then again for every reference to those same students in List<Enrolment>. I'm looking for an elegant way to solve this.

反序列化后,我可以修复由反序列化创建重复对象实例使用的ID值引用但这似乎hackish的。

After deserialization I can fix the references using the ID values in the duplicate object instances created by the deserialization but this seems hackish.

以固定冗余输出一种方法是XmlIgnore Enrolment.Student和Enrolment.Course并创建两个属性序列化 - Enrolment.StudentID和Enrolment.CourseID。但是反序列化过程中,对于Enrolment.Student和Enrolment.Course引用不能因为名单,其中的反序列化的结果集(据我所知);学生&GT; 名单&LT;课程&GT; 不可用

One method to fix the redundant output is to XmlIgnore Enrolment.Student and Enrolment.Course and create two more properties for serialization - Enrolment.StudentID and Enrolment.CourseID. However during deserialization, the references for Enrolment.Student and Enrolment.Course cannot be set (AFAIK) since the results of deserialization of List<Student> and List<Course> are not available.

我想到了另一种方法是序列化低下来我的对象层次,我的每个单独列出做和控制反序列化的顺序 - 我宁愿不做这个

Another method I thought of is to serialize lower down in my object hierarchy doing each of my Lists separately and controlling the order of deserialization - I rather not do this.

另一种方法是将XmlIgnore 名单,其中,报名&GT; 并创建初始化的招生序列化的辅助类名单,其中,报名&GT; 后自身的反序列化完成。这似乎是一个很大的功夫。

Another method would be to XmlIgnore List<Enrolment> and create an enrolment serialization helper class that initializes List<Enrolment> after the deserialization of itself is complete. This seems like a lot of effort.

如何做其他人的序列化/反序列化多引用使用XmlSerializer的同一个对象?

How do other people serialize/deserialize multiple references to the same object using XmlSerializer?

推荐答案

呵呵系列化的痛苦: - > ...

Oh the pains of serialization :-> ...

从来没有为这是一个通用的解决方案,我想这就是为什么MS剥离出来Silverlight的框架。

There was never a generic solution for this, I guess that's why MS stripped it out of the Silverlight framework.

我从来不依赖于.NET Framework的任何自动序列化机制。对于我自己的模式和存储库,我通常都知道,也可以很容易地编程方式确定哪些属性是简单的标量的人(数字/字符串/等),它们是链接到其他对象(以及它们要么列表)。

I never rely on any automatic serialization mechanisms of the .net framework. For my own models and repositories, I usually know or can easily programmatically determine which properties are simple scalar ones (numbers/strings/etc) and which are links to other objects (as well as which are lists of either).

基本上有两种情景:

1:我们要序列化对象/转移只平信息。在这种情况下,我的链接到其他对象的属性只传送各自的ID。然后,接收器可以使随后的查询,以获得他们所需要的所有其他对象。

1: We want to serialize/transfer only the flat information of objects. In that case I transfer only the respective IDs for properties that link to other objects. The receiver can then make subsequent queries to get all other objects they need.

2:我们希望与几个层次传递尽可能多的信息越好,即更深层次的嵌套的XML,多为一些直接使用的XML仅仅是一些CSS的报表功能显示的一切。在这种情况下,它实际上是希望是相同的对象将多次被解析成XML树

2: We want to transfer as much information as possible, i.e. deeper nested XML with several levels, mostly for some reporting functionality displaying everything directly using merely some CSS on the XML. In that case, it is actually desired that objects that are the same will be resolved multiple times into the XML tree.

有时候,我需要调整的第一个场景一点点,以避免过多的后续查询电话,但通常我相处得非常好。即我已经内置到我们code的基础,我们可以指定我们要解决在这额外的对象,和/或它的某个地方进行配置。

Sometimes I need to tweak the first scenario a little bit in order to avoid too many subsequent query calls, but usually I get along very well. I.e. I have built into our code base that we can specify which additional objects we want to resolve when, and/or it's configured somewhere.

这篇关于.NET的XmlSerializer并多次引用同一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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