lake.jpg
XML Document Copy PDF Print E-mail
Written by Regan Pestl   
Tuesday, 24 February 2009 13:13

When working with XML documents, temporary changes to the document are often required when mixing XPath processing and XSLT. For example, in a complex reporting process, you may want to remove all records that are not part of the reporting period or other filter criteria, before running data in XSLT.

In many cases it is important not to make changes to the actual working document to avoid corrupting a cached copy, or saving the changed document to disk. In this scenario, a copy of the document is required.

Making a copy of an XmlDocument is a simple task, however it can cause issues such as "System Out of Memory" exceptions to occur in .NET if not done in an efficient manner.

A document copy is simple concept, but it can be difficult to do in practice. In .NET for example, there is no immediately recognizable "DocumentCopy" function. The easiest solution is to use the following code:

// Load Original Document
XmlDocument xmlOriginal = new XmlDocument();
xmlOriginal.LoadXml("filename.xml");

// Create New Document
XmlDocument xmlCopy = new XmlDocument();

// Copy Contents of Original Document to Copy
xmlCopy.LoadXml(xmlOriginal.OuterXml);

The problem with this approach is primarily a memory issue.

These memory issues tend to surface durring here primarily because of the efficient way that XML is loaded into DOM Documents in .NET. Due to this efficent loading, large XML documents can be a fraction of their original size when stored in memory. However when we attempt to run xmlCopy.LoadXml(xmlOriginal.OuterXml) we are converting our original document into a string, and then back to an XmlDocument.

With a large XmlDocument, this can result in a string that is simply to large for .NET to handle. This causes the occasional persistent "system out of memory" messages.

Actual Scenario

This issue was most recently observed on our Windows 2003 server using IIS 6.0 and .NET 3.5 with a 20MB XML File.

The error message was difficult to recreate, and therefor difficult to debug. This "system out of memory" error message would take hours of normal system use to build, but once it would surface it would resurface continuously until the application pool recycled.

It was observed that on the page in question, xmlCopy.LoadXml(xmlOriginal.OuterXml) was being used, and the error was being thrown from this line of code.

Solution

To avoid loading the entire XmlDocument into a string, we used code similar to the following to append the original document into the new document.

// Load Original Document
XmlDocument xmlOriginal = new XmlDocument();
xmlOriginal.LoadXml("filename");

// Create New Document
XmlDocument xmlCopy = new XmlDocument();

// Copy Contents of Original Document to Copy
xmlCopy.AppendChild(xmlCopy.ImportNode(xmlOriginal.DocumentElement,true));

Last Updated on Wednesday, 25 February 2009 15:12
 

 

Copyright WCG:ITX 2001-2009 - Contact us bill@wcg-itx.com 905 477 5932