Xml Comparison in C#

前端 未结 5 2063
离开以前
离开以前 2020-11-30 05:30

I\'m trying to compare two Xml files using C# code. I want to ignore Xml syntax differences (i.e. prefix names). For that I am using Microsoft\'s XML Diff and Patch C# API.

5条回答
  •  广开言路
    2020-11-30 05:49

    The documents are isomorphic as can be shown by the program below. I think if you use XmlDiffOptions.IgnoreNamespaces and XmlDiffOptions.IgnorePrefixes to configure Microsoft.XmlDiffPatch.XmlDiff, you get the result you want.

    using System.Linq;
    using System.Xml.Linq;
    namespace SO_794331
    {
        class Program
        {
            static void Main(string[] args)
            {
                var docA = XDocument.Parse(
                    @"1");
                var docB = XDocument.Parse(
                    @"1");
    
                var rootNameA = docA.Root.Name;
                var rootNameB = docB.Root.Name;
                var equalRootNames = rootNameB.Equals(rootNameA);
    
                var descendantsA = docA.Root.Descendants();
                var descendantsB = docB.Root.Descendants();
                for (int i = 0; i < descendantsA.Count(); i++)
                {
                    var descendantA = descendantsA.ElementAt(i);
                    var descendantB = descendantsB.ElementAt(i);
                    var equalChildNames = descendantA.Name.Equals(descendantB.Name);
    
                    var valueA = descendantA.Value;
                    var valueB = descendantB.Value;
                    var equalValues = valueA.Equals(valueB);
                }
            }
        }
    }
    

提交回复
热议问题