Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The most performant way to validate XML against XSD

I get a string variable with XML in it and have a XSD file. I have to validate the XML in the string against the XSD file and know there is more than one way (XmlDocument, XmlReader, ... ?).

After the validation I just have to store the XML, so I don't need it in an XDocument or XmlDocument.

What's the way to go if I want the fastest performance?

like image 934
Hinek Avatar asked Sep 08 '10 10:09

Hinek


3 Answers

Others have already mentioned the XmlReader class for doing the validation, and I wont elaborate further into that.

Your question does not specify much context. Will you be doing this validation repeatedly for several xml documents, or just once? I'm reading a scenario where you are just validating a lot of xml documents (from a third party system?) and storing them for future use.

My contribution to the performance hunt would be to use a compiled XmlSchemaSet which would be thread safe, so several threads can reuse it without needing to parse the xsd document again.

var xmlSchema = XmlSchema.Read(stream, null);
var xmlSchemaSet = new XmlSchemaSet();
xmlSchemaSet.Add(xmlSchema);
xmlSchemaSet.Compile();

CachedSchemas.Add(name, xmlSchemaSet);
like image 127
sisve Avatar answered Sep 24 '22 22:09

sisve


I would go for the XmlReader with XmlReaderSettings because does not need to load the complete XML in memory. It will be more efficient for big XML files.

like image 27
Johann Blais Avatar answered Sep 25 '22 22:09

Johann Blais


I think the fastest way is to use an XmlReader that validates the document as it is being read. This allows you to validate the document in only one pass: http://msdn.microsoft.com/en-us/library/hdf992b8.aspx

like image 37
Rune Grimstad Avatar answered Sep 23 '22 22:09

Rune Grimstad