After more profiling I found the cause of the performance loss.
If you are creating elements in an XmlDocument that has an associated
XmlDocumentType then the code searches the XML grammar (DTD or schema) and
creates an default attributes defined by the new element. If the document
has a DTD based grammar then this operation will cause the element
definitions to be parsed on each invocation - hence the loss of performance
I was experiencing.
In my code I added some lines to temporarily remove the XmlDocumentType from
the document before creating the new elements. I re-insert the
XmlDocumentType node when all the changes are complete. The ImportNode
method now executes as it should in milliseconds rather than seconds. I have
not tried profile the ImportNode operation on a document that has an XML
schema defined grammar. Obviously the resulting XmlDocument structure is a
little different, but default attribute values can easily be corrected
during subsequent processing.
For a simple DTD based grammar the performance loss might not as large as it
was in my case (my target DTD was 50K of definitions, my instance document
as 22K).
"Andrew Jacobs" <an****@im.not.telling.you.com> wrote in message
news:9K**************@newsfe2-win.ntli.net...
I am using C# to write an application that migrates documents from one
version of an XML schema/DTD to later one. Much of the document has the
same structure in the two versions and I use XMLDocument.ImportNode to copy
unchanged sub-trees between the old and new document but this takes much
longer than it should.
I profiled the code and found that when ImportNode copies an element it
initialises any default attributes defined for the element in the schema.
This task appears to reinvoke the XML parser (via InnerText) rather than
create object instances directly. The result is that a an operation that
should complete in milliseconds is currently taking 3 seconds per
document.
How can I configure an XmlDocument so that default attributes are not
initialised as XmlElement instances are created?
Thanks