开发者

XML serialization/deserialization best practice: XmlSerializer or Linq To XML?

开发者 https://www.devze.com 2023-03-07 02:37 出处:网络
No开发者_StackOverflow社区wadays, what\'s the best way to serialize/deserialize domain objects into an xml document? XmlSerializer or Linq To XML? What are the pros and cons of each solution?Here\'s t

No开发者_StackOverflow社区wadays, what's the best way to serialize/deserialize domain objects into an xml document? XmlSerializer or Linq To XML? What are the pros and cons of each solution?


Here's the main benefit I see for using Linq to XML nowadays.

XmlSerializer requires a default (parameter-less) constructor. So, if you're doing any kind of inversion of control and passing dependencies into your class via the constructor, you'll need to also have a default constructor that bypasses the injection of those dependencies. That kinda defeats the whole purpose of using constructor injection.

Of course, with Linq to XML, you'll need to write your own serialization code, but I've done that with either a set of methods like FromXml and ToXml or simply an Xml property with getter and setter that do the serialization of exactly the fields that need to be saved. I like having that control in code instead of having to use attributes on some properties to have them ignored.

0

精彩评论

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

关注公众号