简体   繁体   English

序列化对象列表

[英]Serializing list of objects

I have a list of objects, say List<Topic> I want to serialize them. 我有一个对象列表,例如List<Topic>我要序列化它们。 I am using XmlSerializer and it works fine. 我正在使用XmlSerializer,它工作正常。 But everytime it will serialize and deserialize the whole objects, so won't it affect the performance? 但是每次它都会序列化和反序列化整个对象,那么它不会影响性能吗? The list's items may be removed at any time and once it is removed it should be removed from Xml also. 列表中的项目可以随时删除,一旦删除,它也应该从Xml中删除。

This is my current serialization code:- 这是我当前的序列化代码:

  XmlSerializer serializer = new XmlSerializer(typeof(List<Topic>));
                    serializer.Serialize(somestream, topicList);

To deserialize I would simply write:- 要反序列化,我只需写:

  XmlSerializer serializer = new XmlSerializer(typeof(List<Topic>));
                    topicList = serializer.Deserialize(somestream) as List<Topic>;

Is this technique Ok or can we improve performance? 这种技术可以吗?还是可以提高性能? The List typically contains 80-100 items. 该列表通常包含80-100个项目。

Thanks in advance :) 提前致谢 :)

First, have you tested to ensure there is a performance issue? 首先,您是否经过测试以确保存在性能问题?

Second, the XmlSerializer cannot delete the bits of XML that represent the items that have been removed without doing a full serialization to work out what has changed and how that changes the XML. 其次,XmlSerializer不能删除代表已删除项目的XML位,而无需进行完整的序列化来确定已更改的内容以及如何更改XML。

As such, while the performance may or may not be costly depending on your objects, there is no way to serialize/deserialize only part of the list and only make small modifications to the XML based on items changed. 因此,尽管性能可能会很高,也可能不会很昂贵,这取决于对象,但是没有办法只对列表的一部分进行序列化/反序列化,并且仅根据更改的项目对XML进行少量修改。

However, if you are quite regularly serializing/deserializing, and making changes to parts of the list, is it possible in your situation to look at using a database? 但是,如果您非常有规律地进行序列化/反序列化并更改列表的某些部分,那么您可能会考虑使用数据库吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM