How to deserialize unknown type from XML?

VopangepECege

New Member
Consider the following overly simplified chunk of XML:\[code\]<ElementA> <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#integer">5 </AttributeValue></ElementA>\[/code\]Specifically, looking at the AttributeValue element, from the DataType attribute, I know that my value is of type integer (though it could have been a double, string, datetime...any established datatype from the w3 standard). I would like to deserialize this xml to a .NET class with the strongly typed value. The first thing that came to my mind is to create a generic AttributeValue class:\[code\]public class AttributeValue<T>{ public T Value {get; set;}}\[/code\]but of course this won't work for a couple of reasons - the biggest one being I would have to declare the type in the parent class which won't compile because T is not defined:\[code\]public class ElementA{ public AttributeValue<T> {get; set; } // Naturally, this will not work because T} // is not defined.\[/code\]Plus, I would likely have to implement IXmlSerializable on my class to handle the custom serialization. Is there a better way to solve this problem? I know I can serialize the DataType attribute in my code and store the value as a string, then convert later, but it would be helpful to actually have the correct type in my business object for later processingThanks for any help!Jason
 
Back
Top