How can I improve the performance of XML in my applications?
Although a full article can be written on improving XML performance, here are a few considerations for improving performance of your XML related code:
- Use the right XML object for your task:
- Use XmlTextReader to process XML data quickly in a forward, read-only manner.
- Use XmlValidatingReader to process and to validate XML data.
- Use XPathNavigator to obtain read-only, random access to XML data and to use XPath queries.
- Use XmlTextWriter to write XML documents.
- Use XPathDocument to process XPath statements and faster XSLT transformations.
- Validate large documents as much as possible: If there is a chance that a large document may be invalid, consider validating the document first because a lot of CPU resources and memory are wasted when you parse a document which may contain errors.
- Consider element and attribute name lengths: The length of an element or attribute name affects the document size. Try to use names that are short and meaningful.
- On the XmlReader, use the MoveToContent and Skip methods to skip unwanted items: XmlReader.MoveToContent method helps to skip white space, comments, and processing instructions, and to move to the next content element. Also the XmlReader.Skip method helps you skip the current element.
- Do not validate the same document more than once
- Consider caching the schema: If you repeatedly validate input XML against the same schema for each request, then loading the schema once and retaining it in memory for later requests can improve performance.
- Avoid the // operator by reducing the search scope.
Back to
XML FAQ