I’m developing an ASP.NET Web API service, which allows users to post metadata.
In one of the data contracts that the users post, they’re supposed to be able to post their own custom metadata in XML format. This is an example:
<Delivery> <Type>Direct</Type> <Time>12:00:01</Time> <Format>Segmented</Format> <CustomMetadata> <ClientReference>R46375683</ClientReference> <Contact>Me@There.com</Contact> </CustomMetadata> </Delivery>
I’m having trouble creating a data contract that successfully deserializes properly, however. For the CustomMetadata node, I have :
[DataMember(EmitDefaultValue=false)]
public XmlNode CustomMetadata { get; set; }
When it gets deserialized, I receive an exception:
“Collection type ‘System.Xml.XmlNode’ cannot be deserialized since it
does not have a valid Add method with parameter of type
‘System.Object’.”
I don’t fully understand why it’s trying to “Add” anything to the XmlNode, but for whatever reason, it’s failing. Is there an alternative for doing this kind of thing, such as deserializing to a different type? I tried deserializing to a string, but that gave an exception too.
Data Contract serialisation only supports primitives and a few special types. You must use XmlElement instead of XmlNode.
Try:
See http://msdn.microsoft.com/en-us/library/ms733127.aspx