views:

112

answers:

2

I have some very similar XML structures which are in fact quite distinct, but it appears that XmlSerializer.Deserialize is very "forgiving" and will go out of its way to take XML and deserialize out into a strongly typed object I created from the source XSDs. Is there any way to make it more strict or do some type of deeper validation?

// Locals
var serializer = new XmlSerializer(typeof(SomeCustomType));

// Set
var someInstance = serializer.Deserialize(new StringReader(xmlString.ToString()))

@Jeff Because the root nodes are similar it will deserialize into completely different objects. Imagine that you have a house, car and boat and they all share a base root node called item with a few attributes. Even though sub-nodes are invalid and unshared it seems to overlook and forgive that.

@Will I don't want to validate against the XSD. I want to somehow cause the Deserializer to see that the data it has shouldn't be shoe-horned into the wrong Object type.

A: 

The problem was that the XML input was incorrect.

Nissan Fan
A: 

I once used validating reader to validate XML against schema as I read it into the deserializer.

zvolkov