I have a WCF service and cannot use DataContracts since I need more control over the XML received and sent to this service. As such, I use XmlRoot, and XmlElement… the problem I’m running into now is that my class that the receiving xml gets deserialized into and the serialized response both need to have the same root name, and when I try to set both of those classes with:
[XmlRoot(ElementName = "myRoot")]
I get an error saying that the root name was already used. Is there a simple workaround for this? I tried putting my response class in a separate namespace but that didn’t appear to work.
If some of the variables aren’t set in my response class that gets serialized then I don’t them to get serialized and returned in the response… is there an option I’m missing to do this… I was able to do this with a DataContract, but cant figure it out with XmlElements
One way to achieve this is to intercept the XML response and change the root element name to something unique before it is deserialized. This can be done pretty easily with a custom IClientMessageFormatter and an associated attribute on the operation.
I just wrote this up, so “wet paint” and all, but here is what that looks like: