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:
<rootElement>
<header>
<!-- some stuff here -->
</header>
<businessContent>
<oneOfSeveralAllowedSubNodesHereLikeCustomer />
<businessContent>
</rootElement>
I got xsd.exe
to generate C# classes from a schema file I've got and it did it like that (simplified):
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
Notice that the type of businessContent
is System.Object
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 customer
inside its businessContent
. customer is defined in the xsd and the xsd.exe
generated a class for it as well. I would expect an instance of this class to be created and put as my businessContent
but when I read the XML:
var serializer = new XmlSerializer(typeof(rootElement));
var root = (rootElement)serializer.Deserialize(stream));
var customer = (customer)root.businessContent;
I get an exception, because the type of root.businessContent
is XmlNode[]
and not customer
.
So how do I make the serializer to deserialize my object fully, that is with customer
and not XmlNode[]
inside?