I’ve a simple class that contains Properties metadata and I need to pass instances of that class with WCF. Since it contains Dictionary of objects I decided to implement IXmlSerializable to decide the exact format of serialization and make it usable with WCF. Since I really hate reading XML stream with XmlReader, I decided to implement the ReadXml method using an XElement, thanks to the fact that I can create an XElement from a XmlReader thanks to the Load method.
Using an XElement is really better than using the Raw XmlReader interface. I wrote a couple of tests to verify that everything work, then I use this class in a DataContract and sent over the wire with WCF. When I call the WCF function I got a strange error * The XmlReader state should be EndOfFile after this operation.* error. This happens because the serialized XML content sent by WCF is somewhat manipulated and when the XElement read the content of the XmlReader after he finished reading the content the reader is not at the end. This check is somewhat annoying but you can do a dirty trick to avoid this problem.
I’ve changed the ReadXml function only a little, I read all the content of the XmlReader in a String thanks to the ReadInnerXml() method, then I use the standard XElement.Parse() method to create an XElement from a string. Everything works as expected, but I still wonder why the XmlReader that comes from a WCF serialization cannot be read directly in an XElement. o_O