XmlSchemaSet Add xsd file to validate Xdocument takes too long more than one minute

389 views Asked by At

I am validating and Xdocument to and xsd file and everything works fine, but when I add the Xsd file to the XmlSchemaSet object it takes like one minute and a half to load, the schema is not small but only occupies 50kb which in my opinion shouldn't take that much to load. I have tried with other smaller xsd files and load in 1ms. Also tried as an embedding resource with same results. Do I have something wrong or is there a way to have the Xmlschemaset loaded somehow as a class so it doesn't loads every time, any help will be greatly appreciated.

Here is the code

 XmlSchemaSet schemaSet = new XmlSchemaSet();
 schemaSet.Add(null, "c:\\temp\\GT_Documento-0.2.0.xsd");
 XDocument xml = XDocument.Load("c:\\temp\\xmlinvoice.xml");
 xml.Validate(schemaSet, ValidationCallback); 

Xsd link:

https://github.com/fel-sat-gob-gt/cat/blob/desa/xsd/GT_Documento-0.2.0.xsd
2

There are 2 answers

0
Michael Kay On

The schema imports a schema document from the W3C web site at

http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd

W3C actively discourage you from making operational access to such resources because they can't handle the traffic; so they impose an artificial 30-second delay on the request. You need to make a local copy of this resource and adjust your validation to pick up the local copy, either by changing the schemaLocation, or by using some kind of resolver to redirect the request.

0
Jaime On

The problem was as stated, just added a XmlPreloadedResolver using the following code, if there are several xsd you need to add all of them.

var resolver = new XmlPreloadedResolver();
resolver.Add(new Uri("http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core- 
schema.xsd"), File.ReadAllBytes(@"c:\temp\xmldsig.xsd"));
schemaSet.XmlResolver = resolver;
schemaSet.Add(null, "http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd");