XSL
XSL

Reputation: 3055

XML Serialization/Deserialization per item

I'm designing a car catalogue and need to use XML files for storage. In previous projecs, I was manually editing XML files with Linq. However, I came across XML serialization and am thinking this might be a better approach. Each item in the catalogue would be of type CarItem and contain various attributes. 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? 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".

Thanks for any suggestions

Upvotes: 0

Views: 265

Answers (2)

Mick N
Mick N

Reputation: 14882

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.

Shawn Oster's post in this thread I believe is quite close to what you want using linq. You can add where clauses to suit your requirements easily.

Upvotes: 1

Pavel Tupitsyn
Pavel Tupitsyn

Reputation: 9006

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.

Upvotes: 1

Related Questions