Setting Options that Affect the Comparison

Microsoft XML Diff

Microsoft XML Diff 1.0 and XML Patch 1.0

Setting Options that Affect the Comparison

The XmlDiff class allows you to set different options that affect the behavior of the comparison, as well as the resulting XDL Diffgram. The following list describes the enumeration properties that affect what items are included for consideration during the comparison. For more information see the XmlDiffOptions Enumeration.

  • IgnoreComments: Comment nodes are not compared when true.
  • IgnorePI: Processing instructions are not compared when true.
  • IgnoreXmlDecl: The XML declaration is not compared when true.
  • IgnorePrefixes: The prefixes of element and attribute names are not compared when true. When this option is selected, then two names that have the same local name and namespace URI, but have a different prefix, are treated as the same names.
  • IgnoreNamespaces: The namespace URIs of the element and attribute names are not compared when true. This option also implies that the prefixes are ignored. When this option is selected, then two names with the same local name, but have a different namespace URI and prefix, are treated as the same names.
  • IgnoreChildOrder: The order of child nodes of each element is ignored when true. When this option is selected, two nodes with the same value that differ only by their position among sibling child nodes are treated as the same nodes.
  • IgnoreWhitespace: Significant white spaces are not compared when true, and all text nodes are normalized by discarding any leading and trailing white space characters (#x9, #x10, #x13, #x20), and by replacing sequences of white space characters by a single space (#x20) character.
  • IgnoreDtd: The XML DTD is not compared when true.
    Note   The order of attributes is always ignored.

There are several ways to set the options. The following example shows how to set the IgnorePI and IgnoreComments enumerations by using the XmlDiff.Options property explicitly.

[Visual Basic]
Imports System
Imports System.Xml
Imports System.IO
Imports Microsoft.XmlDiffPatch

Namespace TestCompare

   Class Class1

      Shared Sub Main()
        Dim myDiff As New XmlDiff()
        Dim diffgramWriter = New XmlTextWriter(New StreamWriter("diffgram.xml"))
        myDiff.Options = XmlDiffOptions.IgnorePI Or XmlDiffOptions.IgnoreComments
        Dim bSame As Boolean = myDiff.Compare("Source.xml", "Changed.xml", False, diffgramWriter)
        diffgramWriter.Close()
      End Sub                            'Main
   End Class                             'Class1
End Namespace                            'TestCompare 
[C#]
using System;
using System.Xml;
using System.IO;
using Microsoft.XmlDiffPatch;

namespace TestCompare
{
  class Class1
    {
        static void Main()
          {
            XmlDiff myDiff = new XmlDiff();
            XmlWriter diffgramWriter = new XmlTextWriter( new StreamWriter( "diffgram.xml" ) );
            myDiff.Options = XmlDiffOptions.IgnorePI | XmlDiffOptions.IgnoreComments;
            bool bSame = myDiff.Compare("Source.xml", "Changed.xml", false, diffgramWriter);
            diffgramWriter.Close();
            }
    }
}

The following example shows how to run the compare with the XmlDiffOptions enumerations of IgnorePI and IgnoreComments set to true without using the XmlDiff.Options property. Instead, it sets them as a property of the XmlDiff class.

[Visual Basic]
Imports System
Imports System.Xml
Imports System.IO
Imports Microsoft.XmlDiffPatch

Namespace TestCompare

   Class Class1

      Shared Sub Main()
        Dim myDiff As New XmlDiff()
        Dim diffgramWriter = New XmlTextWriter(New StreamWriter("diffgram.xml"))
        myDiff.IgnorePI = True
        myDiff.IgnoreComments = True
        Dim bSame As Boolean = myDiff.Compare("Source.xml", "Changed.xml", False, diffgramWriter)
        diffgramWriter.Close()
      End Sub
   End Class 
End Namespace
[C#]
using System;
using System.Xml;
using System.IO;
using Microsoft.XmlDiffPatch;

namespace TestCompare
{
  class Class1
    {
        static void Main()
            {
              XmlDiff myDiff = new XmlDiff();
              XmlWriter diffgramWriter = new XmlTextWriter( new StreamWriter( "diffgram.xml" ) );
              myDiff.IgnorePI = true;
              myDiff.IgnoreComments = true;
              bool bSame = myDiff.Compare("Source.xml", "Changed.xml", false, diffgramWriter);
              diffgramWriter.Close();
            }
    }
}

This last example shows the options being set inside the constructor of the XmlDiff class, using the XmlDiffOptions, as viewed in the first line of code.

[Visual Basic]
Imports System
Imports System.Xml
Imports System.IO
Imports Microsoft.XmlDiffPatch

Namespace TestCompare

   Class Class1

      Shared Sub Main()
        Dim myDiff As New XmlDiff(XmlDiffOptions.IgnorePI Or XmlDiffOptions.IgnoreComments)
        Dim diffgramWriter = New XmlTextWriter(New StreamWriter("diffgram.xml"))
        Dim bSame As Boolean = myDiff.Compare("Source.xml", "Changed.xml", False, diffgramWriter)
        diffgramWriter.Close()
      End Sub
   End Class
End Namespace
[C#]
using System;
using System.Xml;
using System.IO;
using Microsoft.XmlDiffPatch;

namespace TestCompare
{
  class Class1
    {
        static void Main()
            {
XmlDiff myDiff = new XmlDiff( XmlDiffOptions.IgnorePI | XmlDiffOptions.IgnoreComments);
XmlWriter diffgramWriter = new XmlTextWriter( new StreamWriter( "diffgram.xml" ) );
bool bSame = myDiff.Compare("Source.xml", "Changed.xml", false, diffgramWriter);
diffgramWriter.Close();
            }
    }
}

For information on running the code samples, see Running XmlDiff and XmlPatch Class Code Samples.

See Also

XML Diff Functionality | Running Comparisons Between Documents, Fragments, or Nodes | Selecting the Algorithm for the Comparison | Limitations | XML Diff Language (Diffgram) | Extended Operations | Example of a Diffgram | XmlDiff Class

© 2002 Microsoft Corporation. All rights reserved.