简体   繁体   English

每个项目的XML序列化/反序列化

[英]XML Serialization/Deserialization per item

I'm designing a car catalogue and need to use XML files for storage. 我正在设计汽车目录,需要使用XML文件进行存储。 In previous projecs, I was manually editing XML files with Linq. 在以前的项目中,我是使用Linq手动编辑XML文件的。 However, I came across XML serialization and am thinking this might be a better approach. 但是,我遇到了XML序列化,并认为这可能是更好的方法。 Each item in the catalogue would be of type CarItem and contain various attributes. 目录中的每个项目都将是CarItem类型,并包含各种属性。 The catalogue can contain a few hundred cars and I'm worried about performance. 目录可能包含几百辆汽车,我担心性能。 If I deserialize the XML file, will all the CarItems be instantiated straight away? 如果我反序列化XML文件,是否将立即实例化所有CarItems Is there a way for me to be able to choose which object gets deserialized based on parameters? 有没有一种方法可以让我选择基于参数反序列化的对象? For example, I'd like to say "if car color attribute is red, then only deserialize red CarItems into objects". 例如,我想说“如果汽车颜色属性为红色,则仅将红色CarItems反序列化为对象”。

Thanks for any suggestions 谢谢你的建议

Yes, they all will be instantiated. 是的,它们都将被实例化。 However, few hundred objects is not a big deal for a class with some simple fields. 但是,对于具有一些简单字段的类,几百个对象并不是什么大问题。 Give it a try and check performance. 尝试一下并检查性能。

There's quite a few posts with good examples of how you can control what you pull out and instantiate into objects/scalars using XDocument. 有很多帖子都提供了很好的示例,说明了如何使用XDocument控制如何拉出并实例化为对象/标量。

Shawn Oster's post in this thread I believe is quite close to what you want using linq. 我相信Shawn Oster在线程中的帖子与使用linq想要的非常接近。 You can add where clauses to suit your requirements easily. 您可以添加where子句以轻松满足您的要求。

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

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