Once a programmer decides to implement IXmlSerializable
, what are the rules and best practices for implementing it? I\'ve heard that GetSchema()
sh
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.