XML deserialization of weakly typed objects in .NET

ZrzaTurkl

New Member
Sorry for a crappy title. Feel free to change it to a better one.The problem is this: I have a need to read a set of XML files that look like this:\[code\]<rootElement> <header> <!-- some stuff here --> </header> <businessContent> <oneOfSeveralAllowedSubNodesHereLikeCustomer /> <businessContent></rootElement>\[/code\]I got \[code\]xsd.exe\[/code\] to generate C# classes from a schema file I've got and it did it like that (simplified):\[code\]public class rootElement{ public header header {get;set;} public object businessContent {get;set;}}// other classes like header and classes for the values// allowed within business content, say customer\[/code\]Notice that the type of \[code\]businessContent\[/code\] is \[code\]System.Object\[/code\] which is fair enough. The schema says nothing explicitly about what can actually be put in there. But then I have an xml file which has a \[code\]customer\[/code\] inside its \[code\]businessContent\[/code\]. customer is defined in the xsd and the \[code\]xsd.exe\[/code\] generated a class for it as well. I would expect an instance of this class to be created and put as my \[code\]businessContent\[/code\] but when I read the XML:\[code\]var serializer = new XmlSerializer(typeof(rootElement));var root = (rootElement)serializer.Deserialize(stream));var customer = (customer)root.businessContent;\[/code\]I get an exception, because the type of \[code\]root.businessContent\[/code\] is \[code\]XmlNode[]\[/code\] and not \[code\]customer\[/code\].So how do I make the serializer to deserialize my object fully, that is with \[code\]customer\[/code\] and not \[code\]XmlNode[]\[/code\] inside?
 
Back
Top