开发者

What would be the best way to validate XML?

开发者 https://www.devze.com 2022-12-17 18:01 出处:网络
I been looking at XML Serialization for C# and it looks interesting. I was reading this tutorial http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization

I been looking at XML Serialization for C# and it looks interesting. I was reading this tutorial

http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization

and of course you can de serialize it back to a list of objects. So I am wondering would it be better to de serialize it back to to a list of objects and then go through each object and validate it or validate it by using a schema then de serializing 开发者_C百科it and doing stuff with it?

http://support.microsoft.com/kb/307379

Thanks


I guess it would depend a bit on what you want to validate, and for what purpose. If it is intended for interop to other systems, then validating via xsd is a reasonable idea not least because you can use xsd.exe to write your classes for you from the xsd (you can also generate xsd from xml or dll, but it isn't as accurate). Likewise you can use XmlReader (appropriately configured) to check against xsd,

If you just want valid .NET objects, I'd be tempted to leave the serialized form as an implementation detail, and write some C# validation code - perhaps implementing IDataErrorInfo, or using data-annotations.


You can create an XmlValidatingReader and pass that into your serializer. That way you can read the file in one pass and validate it at the same time.

I believe the same technique will work even if you are using hand rolled XML classes (for extremely large XML files) so you might find it worth a look.

Edit:

Sorry just reread some of my code, XmlValidatingReader is obsolete, you can do what you need with the XmlReader.

See XmlReader Settings


For speed I would do it in C#, however for completeness you might want to do it using an XSD. The issue with that is you have to learn the verbose and cumbersome XSD syntax, which from experience takes a lot of trial and error, is time consuming and holds not a lot of reward for serialization. Particularly with constants where you have to map them in C# and also in the XSD.

You'll always be writing the XML as C#. Anything not known when read back in is simply ignored. If you aren't editing the XML with a text editor you can guarantee that it will come back in the right way, in which case XSD is definitely not needed.


If you validate the XML, you can only prove that it's structurally correct. An attempt to deserialize from the XML will tell you the same thing.

Typically business objects can implement business logic/rules/conditions that go beyond a valid schema. That type of knowledge should stay with the business objects themselves, rather than being duplicated in some sort of external validation routine (otherwise, if you change a business rule, you have to update the validator at the same time).

0

精彩评论

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

关注公众号