开发者

XML serialization or reading from XML Objects?

开发者 https://www.devze.com 2023-02-27 06:44 出处:网络
I have different XML files that I will need to read.I\'m wondering if I should deserialize the files into custom objects or just read the data using XDocument objects and Linq-to-XML.

I have different XML files that I will need to read. I'm wondering if I should deserialize the files into custom objects or just read the data using XDocument objects and Linq-to-XML.

The files range in size from 1-2kb to 3mb+, and the different objects also range in complexity (some have attributes, some have children, some both, some none).

I figure it would be easier to work with the objects as opposed to Linq-to-XML, but creating those objects would require some time up开发者_如何转开发 front. Are there any rules of thumb or suggestions about when to deserialize as opposed to Linq?

Thanks for any help!


It really depends on what you are doing with the data. If you are not using all of the information that is provided by the XML document, then a LINQ based approach is probably easiest. Think of taking an RSS feed, and only keeping track of the article dates, and nothing else. In this case using a deserialization technique doesn't really do anything for you.

If you are using just about every last bit of data in the XML document, and its structure reflects that of your object model, then certainly deserialize it. This is something that I do all of the time for things like settings files, and even simple file formats.

In your case it sounds like it already exists, and was created by some external source, and you don't have an object representation of the data in your code already, so I would suggest using a LINQ based approach. Additionally, you mention a lot of variation in the files so the flexibility of LINQ would again come in handy. That is a wild guess based on your description though.


You could use the xsd.exe tool which could generate those classes from you given an XML file:

C:\work>xsd test.xml
C:\work>xsd /classes test.xsd

There is no really a rule of thumb. Personally I prefer working with strongly typed objects unless the file sizes become large in which case I switch to XmlReader.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号