If you have the following XHTML text, it is slow to load in. NET with Xmldocument.loadxml.
- <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head>
- <title></title>
- </head>
- <body>
- <P>hello. </p>
- </body>
- </html>
Simple solution: Do not parse external resources
The difference between this XML literal and the generic XML text is a DOCTYPE description that specifies a DTD file. Under. NET documentation for Xmldocument.loadxml is not a DTD or schema validation, so it is not the validation that consumes time. The same is true for the load method.
However, this XML text comparison involves an external text, HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-STRICT.DTD, so Microsoft will load the file first when it loads. That is, when loading each XHTML file, it will go to www.w3.org to download a file.
XmlDocument is to set up how to handle external resources by XmlResolver this property. Simply by setting this property to null(Nothing), you can completely not parse any external resources. The speed is fast.
However, this workaround is not a complete solution, because all entities (entities) are not properly interpreted, such as ©. If you confirm that the input text is not entities, it can be solved in such a simple way.
Perfect solution: Save an XHTML trial DTD to a local
The perfect solution is to save the DTD-related files that XHTML uses to the local directory, allowing XmlDocument to read the entities definition information locally. There are 6 XHTML-related DTD files, listed in the XHTML 1.0 standard Appendix: DTDs, listing the file URLs as follows.
- Xhtml-1.0-strict:http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd
- Xhtml-1.0-transitional:http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd
- Xhtml-1.0-frameset:http://www.w3.org/tr/xhtml1/dtd/xhtml1-frameset.dtd
- Latin-1 characters:http://www.w3.org/tr/xhtml1/dtd/xhtml-lat1.ent
- Special Characters:http://www.w3.org/tr/xhtml1/dtd/xhtml-special.ent
- Symbols:http://www.w3.org/tr/xhtml1/dtd/xhtml-symbol.ent
The above files need to be saved to a local directory, and the following code is used to establish the Xhtmlurlresolver class to redirect XHTML-related DTD files to the local directory.
Loadxml loading XHTML files is slow