I have a web application that generates a medium sized XML dataset to be consumed by a third party. I thought it would be a good idea to provide some form of schema document for the XML that I generate so I pasted the XML into Visual Studio and got it to generate an XSD. The annoying thing is that my XML doesn't validate to the XSD that was generated!

Is it better to roll your own XSD? What about different schema docs like DTDs, Relax NG, or Schematron? The key is that I would like to be able to validate my document using C#. What are your xml validation strategies?


You will be able to validate your XML with either an XML Schema or a DTD using C#. DTDs are older standards as compared to XML Schemas.

So, I recommend an XML Schema approach.

+3  A: 

Whether you choose XSD and/or Schematron depends on what you are trying to validate. XSD is probably the most common validation strategy, but there are limits on what it can validate. If all you want to do is ensure that the right type of data is in each field, XSD should work for you. If you need to assert, for example, that the value of the <small> element is less than the value of the <big> element, or even more complex business rules involving multiple fields, you probably want Schematron or a hybrid approach.

Nicholas Trandem

Thanks guys! So I've decided to stick with XSD, so what are your strategies for creating an XSD? Do you automate it, or do you write it by hand?

Edit - I ended up writing the XSD by hand. It only took a few hours so I guess it wasn't that big of a deal.

Scott Muc