
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Validating Modified XML Documents Without Reloading
|
865
}
// Add it in.
invoice.DocumentElement.AppendChild(items.Clone( ));
One item to be aware of when dealing with multiple XmlDocuments is that when you
take a node from one
XmlDocument, you cannot just append it as a child to a node in a
different
XmlDocument because the node has the context of the original XmlDocument. If
you try to do this, you will get the following exception message:
The node to be inserted is from a different document context.
To fix this, use the XmlDocument.ImportNode method, which will make a copy (deep or
shallow) of the node you are bringing over to the new
XmlDocument. For instance,
when you add the shipping information like so:
invoice.DocumentElement.AppendChild(invoice.ImportNode(shipInfo,true));
this line takes the shipInfo node, clones it deeply, then appends it to the main
invoice node.
See Also
See the “XmlDocument Class,” “XmlElement Class,” and “XmlAttribute Class” top-
ics in the MSDN documentation.
15.11 Validating Modified XML Documents
Without Reloading
Problem
You are using the XmlDocument to modify an XML document loaded in memory.
Once the document has been modified, the modifications need to be verified and
schema defaults need to be enforced.
Solution
Use the XmlDocument.Validate method to perform ...