Proper way to implement IXmlSerializable?

前端 未结 5 545
逝去的感伤
逝去的感伤 2020-11-22 05:34

Once a programmer decides to implement IXmlSerializable, what are the rules and best practices for implementing it? I\'ve heard that GetSchema() sh

5条回答
  •  Happy的楠姐
    2020-11-22 06:32

    Yes, the whole thing is a bit of a minefield, isn't it? Marc Gravell's answer pretty much covers it, but I'd like to add that in a project I worked on we found it quite awkward to have to manually write the outer XML element. It also resulted in inconsistent XML element names for objects of the same type.

    Our solution was to define our own IXmlSerializable interface, derived from the system one, which added a method called WriteOuterXml(). As you can guess, this method would simply write the outer element, then call WriteXml(), then write the end of the element. Of course, the system XML serializer wouldn't call this method, so it was only useful when we did our own serialization, so that may or may not be helpful in your case. Similarly, we added a ReadContentXml() method, which didn't read the outer element, only its content.

提交回复
热议问题