XML Patch Functionality
The Microsoft XML Patch enables you to take an XDL Diffgram produced from the Microsoft XML Diff tool, and apply it to the original source document to recreate the changed document.
This is useful if you have multiple source documents at various physical locations, and one of these documents changes. You may use the XDL Diffgram and XML Patch tool to propagate the change to all the other source documents by applying the XDL Diffgram to them.
A custom application could use the XDL Diffgram as a file that shows modifications made to a source document, similar to change tracking for audit purposes. Whatever your need, the XML Patch tool allows the use of the XDL Diffgram to create the changed document, fragment or node from the original source document, fragment or node.
The XmlPatch class is the class that performs the document, fragment or node modification. It uses the Patch method to apply the XDL Diffgram to a source document to create a patched document. There are several overloaded Patch methods offered, each reading in the source document from different formats, and then saving the patched document in different formats. For more information on the overloaded Patch methods, see XmlPatch.Patch Method.
The following code sample loads a source document and an XDL Diffgram, and saves the changed sourceDoc into a new file called changed_doc.xml.
[Visual Basic] Imports System Imports System.IO Imports System.Xml Imports Microsoft.XmlDiffPatch Public Class Sample Public Shared Sub Main() Dim sourceDoc As New XmlDocument() sourceDoc.Load("source.xml") Dim myPatch As New XmlPatch() Dim myRdr As New XmlTextReader("diffgram.xml") myPatch.Patch(sourceDoc, myRdr) sourceDoc.Save("changed_doc.xml") End Sub 'Main End Class 'Sample [C#] using System; using System.IO; using System.Xml; using Microsoft.XmlDiffPatch; public class Sample { public static void Main() { XmlDocument sourceDoc = new XmlDocument(); sourceDoc.Load("source.xml"); XmlPatch myPatch = new XmlPatch(); XmlTextReader myRdr = new XmlTextReader("diffgram.xml"); myPatch.Patch(sourceDoc, myRdr); sourceDoc.Save("changed_doc.xml"); } }
For more information on running the code sample, see Running XmlDiff and XmlPatch Class Code Samples.
One Patch method takes an XmlDocument as the source document, and applies the XDL Diffgram that is parsed by a derived class of the XmlReader. The Patch method will reject a document with entity references that is loaded into the XmlDocument, and whose XDL Diffgram has been generated by a Compare that compared documents parsed by an XmlTextReader, as that XDL Diffgram will not have expanded entity references. The Patch method in the XmlPatch class will reject a DOM document loaded into XmlDocument. That is, if it contains expanded entity references and if the XDL Diffgram supplied for patching has been generated by a Compare method that takes document files or XmlReaders as input parameters.
For more information on the XmlPatch methods and properties, see XmlPatch Class.
See Also
Xml Diff Functionality | XML Diff Language (Diffgram) | Microsoft.XMLDiffPatch Namespace