Do you avoid validating XML documents unnecessarily?
Updated by Brady Stroud [SSW] 1 year ago. See history
123
Validating an XML document against a schema is expensive, and should not be done where it is not absolutely necessary. Combined with weight the XML document object, validation can cause a significant performance hit:
- Read with XmlValidatingReader: 172203 nodes - 812 ms
- Read with XmlTextReader: 172203 nodes - 320 ms
- Parse using XmlDocument no validation - length 1619608 - 1052 ms
- Parse using XmlDocument with XmlValidatingReader: length 1619608 - 1862 ms
You can disable validation when using the XmlDocument object by passing an XmlTextReader instead of the XmlValidatingTextReader:
XmlDocument report = new XmlDocument();XmlTextReader tr = new XmlTextReader(Configuration.LastReportPath);report.Load(tr);
To perform validation:
XmlDocument report = new XmlDocument();XmlTextReader tr = new XmlTextReader(Configuration.LastReportPath);XmlValidatingReader reader = new XmlValidatingReader(tr);report.Load(reader);
The XSD should be distributed in the same directory as the XML file and a relative path should be used:
<Report> <Report xmlns="LinkAuditorReport.xsd">... </Report>