Home
Do you avoid validating XML documents unnecessarily?
  v1.0 Posted at 26/04/2018 5:41 AM by Tiago Araujo

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>​​

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments:

    Note: Social Media login for Yotpo is not working in IE or Safari, please use Chrome. We are waiting for Yotpo to fix it.