开发者

How can I make XmlSerializer.Deserialize more strict?

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 typ开发者_Go百科e.


The problem was that the XML input was incorrect.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜