views:

488

answers:

2

In my project I have very big XSD file which i use to validate some XML request and response to a 3rd party.

For the above scenario I can have 2 approaches

1) Create XML and then validate against give XSD 2) Create classes from XSD with the help of XSD gen tool, add xtra bit of attirbutes and use them for validation.

Validation in the second way will work somewhat in this manner, a) convert xml request/response into object with XML Serialization b) validate the object with custom attributes set on each property, i.e. Pass the object to a method which will validate the object by iterating through properties and its custom attributes set on the each property, and this will return a boolean value if the object validates and that determines whether the xml request is valid or not?

Now the concern which approach is good in terms of performance and anything else???

A: 

I'm not sure that deserializing your XML into an object is going to give you the validation you require.

Take, for example, an XSD that defines a string field and then constrains the text that can appear in that field to match some regular expression. Will your generated class honour that constraint? There are a number of subtle issues like this that might leave you in a situation where you are either accepting invalid xml, or rejecting otherwise valid xml.

If your 3rd party has given you an XSD to validate against then it's probably best that you stick to this contract definition rather than find short cuts.

HOWEVER, you may well find that there are common mistakes and errors that you can filter out quickly. It all depends upon your signal to noise ratio, but you might consider creating a simple XSD or a programatic test that can "fail fast" before you then invest time on running the full XSD. However, this will only make make sense if you get a lot of failures and the cost of fully validating with an XSD is high.

Also, ensure you're using the fastest XSD validation for your scenario. You've not said if this is a .NET environment or not, but if it is then you have XmlDocument, XmlValidatingReader and XElement as 3 ways of reading XML and validating it against scheme. Depending on where you've getting the XML from, what you're doing with it afterwards, etc you should evaluate which of these mechanisms gives you the best perf.

Martin Peck
A: 

If your primary concern is performance you should use the XmlReader with the XSD schema attached to it for validation. Here is an example:

// Store a reference to this object
// to reuse the compiled XSD schema
// for multiple parsing operations
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("http://www.contoso.com/books", "books.xsd");
settings.ValidationType = ValidationType.Schema;

using (XmlReader reader = XmlReader.Create("books.xml", settings))
{
    while (reader.Read())
    {
        // Do parsing logic
    }
}
Enrico Campidoglio