Loadxml loading XHTML files is slow

Source: Internet
Author: User

If you have the following XHTML text, it is slow to load in. NET with Xmldocument.loadxml.

  1. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head>
  4. <title></title>
  5. </head>
  6. <body>
  7. <P>hello. </p>
  8. </body>
  9. </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 &copy;. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.