Example #1
0
        public static bool AreEqual(String expected, String actual)
        {
            XmlDiff xmlDiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder);

            XmlDocument expectedDocument = new XmlDocument();
            XmlDocument actualDocument   = new XmlDocument();

            expectedDocument.LoadXml(expected);
            actualDocument.LoadXml(actual);

            return(xmlDiff.Compare(expectedDocument, actualDocument));
        }
Example #2
0
        /// <summary>
        /// Compare 2 XML strings, ignoring comments, white spaces and xml declaration and child order.
        /// </summary>
        /// <returns>Ture if the same.</returns>
        protected bool CompareXml(XElement x1, XElement x2)
        {
            XmlDiff diff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder | XmlDiffOptions.IgnoreComments
                                       | XmlDiffOptions.IgnoreWhitespace | XmlDiffOptions.IgnoreXmlDecl
                                       | XmlDiffOptions.IgnoreNamespaces);

            //XElement may otimized the presentation of namespaces, thus sub elements with the same namespace will have namespace text removed.
            // And XmlDiff seems not be able to deal with this.
            // Comparing namespace is not so important.

            return(diff.Compare(x1.CreateReader(), x2.CreateReader()));
        }
Example #3
0
        public static bool CompareXml(Stream expectedStream, Stream actualStream, string xmldiffoptionvalue, DelayedWriteLogger logger)
        {
            bool bResult = false;

            // Default Diff options used by XSLT V2 driver.
            int     defaultXmlDiffOptions = (int)(XmlDiffOption.InfosetComparison | XmlDiffOption.IgnoreEmptyElement | XmlDiffOption.IgnoreAttributeOrder);
            XmlDiff diff = new XmlDiff();

            if (xmldiffoptionvalue == null || xmldiffoptionvalue.Equals(string.Empty))
            {
                diff.Option = (XmlDiffOption)defaultXmlDiffOptions;
            }
            else
            {
                if (logger != null)
                {
                    logger.LogMessage("Custom XmlDiffOptions used. Value passed is " + xmldiffoptionvalue);
                }
                diff.Option = (XmlDiffOption)int.Parse(xmldiffoptionvalue);
            }

            XmlParserContext context = new XmlParserContext(new NameTable(), null, "", XmlSpace.None);

            try
            {
                bResult =
                    diff.Compare(new XmlTextReader(actualStream, XmlNodeType.Element, context),
                                 new XmlTextReader(expectedStream, XmlNodeType.Element, context));
            }
            catch (Exception e)
            {
                bResult = false;
                if (logger != null)
                {
                    logger.LogMessage("Exception thrown in XmlDiff compare!");
                    logger.LogXml(e.ToString());
                    throw;
                }
            }

            if (bResult)
            {
                return(true);
            }

            if (logger != null)
            {
                logger.LogMessage("Mismatch in XmlDiff");
                logger.LogMessage("Actual result: ");
            }

            return(false);
        }
Example #4
0
        private void SaveWithFile(object doc)
        {
            string file0 = GenerateTestFileName(0);
            string file1 = GenerateTestFileName(1);
            string file2 = GenerateTestFileName(2);

            foreach (Encoding encoding in GetEncodings())
            {
                if (doc is XDocument)
                {
                    ((XDocument)doc).Save(FilePathUtil.getStream(file0));
                    ((XDocument)doc).Save(FilePathUtil.getStream(file1), SaveOptions.DisableFormatting);
                    ((XDocument)doc).Save(FilePathUtil.getStream(file2), SaveOptions.None);
                }
                else if (doc is XElement)
                {
                    ((XElement)doc).Save(FilePathUtil.getStream(file0));
                    ((XElement)doc).Save(FilePathUtil.getStream(file1), SaveOptions.DisableFormatting);
                    ((XElement)doc).Save(FilePathUtil.getStream(file2), SaveOptions.None);
                }
                else
                {
                    TestLog.Compare(false, "Wrong object");
                }
                TestLog.Compare(_diff.Compare(FilePathUtil.getStream(BaseSaveFileName), FilePathUtil.getStream(file0)), "Save failed:encoding " + encoding);
                TestLog.Compare(_diff.Compare(FilePathUtil.getStream(BaseSaveFileName), FilePathUtil.getStream(file1)), "Save(preserveWhitespace true) failed:encoding " + encoding);
                TestLog.Compare(_diff.Compare(FilePathUtil.getStream(BaseSaveFileName), FilePathUtil.getStream(file2)), "Save(preserveWhitespace false) " + encoding);
            }
        }
Example #5
0
        public void record_unpublishable_to_hub_and_publishable_to_gov()
        {
            var record         = GetRecordFromFile("85a9bbdc-2397-4f7c-a71e-0480b26b8807", @"records.85a9bbdc-2397-4f7c-a71e-0480b26b8807.json");
            var expectedXmlDoc = GetInputFileAsXmlDoc(@"wafs.85a9bbdc-2397-4f7c-a71e-0480b26b8807.xml");

            var xmlHelper    = new XmlHelper();
            var actualWaf    = xmlHelper.GetMetadataDocument(record);
            var actualXmlDoc = GetByteArrayAsXmlDoc(actualWaf);

            XmlDiff xmlDiff = new XmlDiff();

            Assert.True(xmlDiff.Compare(expectedXmlDoc, actualXmlDoc));
        }
Example #6
0
        public void metadata_document_generated_correctly_for_record_with_url_resource()
        {
            var record         = GetRecordFromFile("9d9775da-44b1-4b96-9302-c842958e9130", @"records.9d9775da-44b1-4b96-9302-c842958e9130.json");
            var expectedXmlDoc = GetInputFileAsXmlDoc(@"wafs.9d9775da-44b1-4b96-9302-c842958e9130.xml");

            var xmlHelper    = new XmlHelper();
            var actualWaf    = xmlHelper.GetMetadataDocument(record);
            var actualXmlDoc = GetByteArrayAsXmlDoc(actualWaf);

            XmlDiff xmlDiff = new XmlDiff();

            Assert.True(xmlDiff.Compare(expectedXmlDoc, actualXmlDoc));
        }
Example #7
0
        public void metadata_document_generated_correctly_for_record_with_multiple_resources()
        {
            var record         = GetRecordFromFile("00b0b44c-a062-4a25-b344-2be12b03a6b5", @"records.00b0b44c-a062-4a25-b344-2be12b03a6b5.json");
            var expectedXmlDoc = GetInputFileAsXmlDoc(@"wafs.00b0b44c-a062-4a25-b344-2be12b03a6b5.xml");

            var xmlHelper    = new XmlHelper();
            var actualWaf    = xmlHelper.GetMetadataDocument(record);
            var actualXmlDoc = GetByteArrayAsXmlDoc(actualWaf);

            XmlDiff xmlDiff = new XmlDiff();

            Assert.True(xmlDiff.Compare(expectedXmlDoc, actualXmlDoc));
        }
Example #8
0
        private void CompareResults(String name)
        {
            PdfReader  reader = new PdfReader(OUT + name + ".pdf");
            string     orig   = RESOURCES + "xml\\test" + name + ".xml";
            string     curr   = TARGET + "xml\\test" + name + ".xml";
            FileStream xmlOut = new FileStream(curr, FileMode.Create);

            new MyTaggedPdfReaderTool().ConvertToXml(reader, xmlOut);
            xmlOut.Close();
            XmlDiff xmldiff = new XmlDiff(XmlDiffOptions.None);

            Assert.True(xmldiff.Compare(orig, curr, false));
        }
        public void metadata_document_generated_correctly_for_record_without_resources()
        {
            var record         = GetRecordFromFile(new Guid("c6f3632d-8789-460b-a09d-c132841a7190"), @"records.c6f3632d-8789-460b-a09d-c132841a7190.json");
            var expectedXmlDoc = GetInputFileAsXmlDoc(@"wafs.c6f3632d-8789-460b-a09d-c132841a7190.xml");

            var xmlHelper    = new OpenDataXmlHelper();
            var actualWaf    = xmlHelper.GetMetadataDocument(record, "");
            var actualXmlDoc = GetByteArrayAsXmlDoc(actualWaf);

            XmlDiff xmlDiff = new XmlDiff();

            Assert.True(xmlDiff.Compare(expectedXmlDoc, actualXmlDoc));
        }
Example #10
0
        public void record_publishable_to_hub_and_gov()
        {
            var record         = GetRecordFromFile("64b5f778-c098-4474-a36e-7f4b2bdfd10b", @"records.64b5f778-c098-4474-a36e-7f4b2bdfd10b.json");
            var expectedXmlDoc = GetInputFileAsXmlDoc(@"wafs.64b5f778-c098-4474-a36e-7f4b2bdfd10b.xml");

            var xmlHelper    = new XmlHelper();
            var actualWaf    = xmlHelper.GetMetadataDocument(record);
            var actualXmlDoc = GetByteArrayAsXmlDoc(actualWaf);

            XmlDiff xmlDiff = new XmlDiff();

            Assert.True(xmlDiff.Compare(expectedXmlDoc, actualXmlDoc));
        }
        public void metadata_document_generated_correctly_for_record_with_additional_resource()
        {
            var record         = GetRecordFromFile(new Guid("4cb2cca3-ec95-4962-9618-8556d88390fd"), @"records.4cb2cca3-ec95-4962-9618-8556d88390fd.json");
            var expectedXmlDoc = GetInputFileAsXmlDoc(@"wafs.4cb2cca3-ec95-4962-9618-8556d88390fd.xml");

            var xmlHelper    = new OpenDataXmlHelper();
            var actualWaf    = xmlHelper.GetMetadataDocument(record, "");
            var actualXmlDoc = GetByteArrayAsXmlDoc(actualWaf);

            XmlDiff xmlDiff = new XmlDiff();

            Assert.True(xmlDiff.Compare(expectedXmlDoc, actualXmlDoc));
        }
        public void metadata_document_generated_correctly_for_record_with_resources()
        {
            var record         = GetRecordFromFile(new Guid("721643b8-7e42-40ca-87d9-23f19221238e"), @"records.721643b8-7e42-40ca-87d9-23f19221238e.json");
            var expectedXmlDoc = GetInputFileAsXmlDoc(@"wafs.721643b8-7e42-40ca-87d9-23f19221238e.xml");

            var xmlHelper    = new OpenDataXmlHelper();
            var actualWaf    = xmlHelper.GetMetadataDocument(record, "http://data.jncc.gov.uk/data/721643b8-7e42-40ca-87d9-23f19221238e-Scotia-Herring-Acoustic-Grab.zip");
            var actualXmlDoc = GetByteArrayAsXmlDoc(actualWaf);

            XmlDiff xmlDiff = new XmlDiff();

            Assert.True(xmlDiff.Compare(expectedXmlDoc, actualXmlDoc));
        }
Example #13
0
        public void metadata_document_generated_correctly_for_record_with_file_resource()
        {
            var record         = GetRecordFromFile("721643b8-7e42-40ca-87d9-23f19221238e", @"records.721643b8-7e42-40ca-87d9-23f19221238e.json");
            var expectedXmlDoc = GetInputFileAsXmlDoc(@"wafs.721643b8-7e42-40ca-87d9-23f19221238e.xml");

            var xmlHelper    = new XmlHelper();
            var actualWaf    = xmlHelper.GetMetadataDocument(record);
            var actualXmlDoc = GetByteArrayAsXmlDoc(actualWaf);

            XmlDiff xmlDiff = new XmlDiff();

            Assert.True(xmlDiff.Compare(expectedXmlDoc, actualXmlDoc));
        }
Example #14
0
        public void CreateDiff(string originalFile, string compareFile, string diffFileName)
        {
            XmlTextWriter tw = new XmlTextWriter(new StreamWriter(diffFileName));

            tw.Formatting = Formatting.Indented;
            try
            {
                XmlDiff diff = new XmlDiff();
                diff.Compare(originalFile, compareFile, false, tw);
            }
            catch { }
            finally { tw.Close(); }
        }
Example #15
0
        public XmlDocument Porovnej()
        {
            XmlDocument xdd   = null;
            string      sTemp = Path.GetTempFileName();
            XmlWriter   xw    = new XmlTextWriter(sTemp, Encoding.UTF8);
            XmlDiff     xd    = new XmlDiff();

            if (xd.Compare(mxdLevy.DocumentElement, mxdPravy.DocumentElement, xw))
            {
                xdd = new XmlDocument();
                xdd.Load(sTemp);
            }
            return(xdd);
        }
Example #16
0
        static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.Error.WriteLine("Usage: program.exe <web config XML> <transformation XDT> <expected XML>");
            }
            var webConfigXml = File.ReadAllText(args[0]);
            var transformXml = File.ReadAllText(args[1]);

            var expectedDoc = new XmlDocument();

            expectedDoc.Load(args[2]);
            using (var document = new XmlTransformableDocument())
            {
                document.PreserveWhitespace = true;
                document.LoadXml(webConfigXml);

                using (var transform = new XmlTransformation(transformXml, false, null))
                {
                    if (transform.Apply(document))
                    {
                        var xmlWriterSettings = new XmlWriterSettings();
                        xmlWriterSettings.Indent      = true;
                        xmlWriterSettings.IndentChars = "    ";

                        var  diffBuilder = new StringBuilder();
                        bool bIdentical;
                        using (var diffgramWriter = XmlWriter.Create(diffBuilder, xmlWriterSettings))
                        {
                            var xmldiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder |
                                                      XmlDiffOptions.IgnoreNamespaces |
                                                      XmlDiffOptions.IgnorePrefixes);
                            bIdentical = xmldiff.Compare(expectedDoc.DocumentElement, document.DocumentElement,
                                                         diffgramWriter);
                        }
                        if (!bIdentical)
                        {
                            Console.Error.WriteLine("Actual differs from expected. Differences are:");
                            Console.Error.WriteLine(diffBuilder.ToString());
                            Environment.Exit(1);
                        }
                    }
                    else
                    {
                        Console.Error.WriteLine("Transformation failed for unkown reason");
                        Environment.Exit(2);
                    }
                }
            }
        }
        public void waf_index_document_generated_correctly()
        {
            var record           = GetRecordFromFile(new Guid("721643b8-7e42-40ca-87d9-23f19221238e"), @"records.721643b8-7e42-40ca-87d9-23f19221238e.json");
            var initialIndex     = GetInputFileContents(@"wafs.index_initial.html");
            var expectedIndexDoc = GetInputFileAsXmlDoc(@"wafs.index_expected.html");

            var xmlHelper      = new OpenDataXmlHelper();
            var actualWaf      = xmlHelper.UpdateWafIndexDocument(record, initialIndex);
            var actualIndexDoc = GetStringAsXmlDoc(actualWaf);

            XmlDiff xmlDiff = new XmlDiff();

            Assert.True(xmlDiff.Compare(expectedIndexDoc, actualIndexDoc));
        }
Example #18
0
        public static bool CompareXml(string originalFile, string finalFile)
        {
            using (var stringWriter = new StringWriter())
                using (var writter = XmlWriter.Create(stringWriter))
                {
                    XmlDiff xmldiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder |
                                                  XmlDiffOptions.IgnoreNamespaces |
                                                  XmlDiffOptions.IgnorePrefixes);

                    var xmlReaderOrigin  = XmlReader.Create(new StringReader(originalFile));
                    var xmlReaderDestiny = XmlReader.Create(new StringReader(finalFile));

                    return(xmldiff.Compare(xmlReaderOrigin, xmlReaderDestiny));
                }
        }
Example #19
0
        public XElement GenerateDiffGram(XElement element1, XElement element2)
        {
            using (var node1Reader = element1.CreateReader())
                using (var node2Reader = element2.CreateReader()) {
                    var result = new XDocument();
                    using (var writer = result.CreateWriter()) {
                        var diff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder | XmlDiffOptions.IgnoreWhitespace | XmlDiffOptions.IgnoreComments | XmlDiffOptions.IgnoreXmlDecl);
                        diff.Compare(node1Reader, node2Reader, writer);
                        writer.Flush();
                        writer.Close();
                    }

                    return(result.Root);
                }
        }
Example #20
0
            string DiffXml(Stream changedXml)
            {
                var differ = new XmlDiff(XmlDiffOptions.None);

                using var diffStream    = new MemoryStream();
                using var diffWriter    = XmlWriter.Create(diffStream, new XmlWriterSettings { CloseOutput = false });
                using var inputReader   = XmlReader.Create(datafile.GetDatafileStream());
                using var changedReader = XmlReader.Create(changedXml);
                var result = differ.Compare(inputReader, changedReader, diffWriter);

                diffWriter.Flush();
                diffStream.Position  = 0;
                using var diffReader = new StreamReader(diffStream, leaveOpen: true);
                return(result ? null : diffReader.ReadToEnd());
            }
Example #21
0
        public void FileConversionTest()
        {
            XmlDocument expectedDocument = new XmlDocument();

            expectedDocument.Load(expectedFilePath);
            BuddyAddonHandler buddyAddonHandler = new BuddyAddonHandler(testPatchesPath);
            XMLPatchHandler   patchHandler      = new XMLPatchHandler(buddyAddonHandler.ModifiedFiles, buddyAddonHandler.Addons);
            var     documentContext             = patchHandler.GenerateXMLDocument();
            XmlDiff diff = new XmlDiff
            {
                Options = XmlDiffOptions.IgnoreWhitespace | XmlDiffOptions.IgnoreComments | XmlDiffOptions.IgnoreXmlDecl | XmlDiffOptions.IgnoreChildOrder
            };

            Assert.IsTrue(diff.Compare(expectedDocument, documentContext));
        }
        public static bool Comapare(string sourcePath, string targetPath, out string diffGram)
        {
            var dg   = new StringBuilder();
            var diff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder | XmlDiffOptions.IgnoreWhitespace | XmlDiffOptions.IgnoreComments | XmlDiffOptions.IgnorePrefixes);

            bool same;

            using (var diffGramWriter = XmlWriter.Create(dg))
            {
                same = diff.Compare(sourcePath, targetPath, false, diffGramWriter);
            }

            diffGram = dg.ToString();

            return(same);
        }
        private bool CompareXmlFiles(string expectationFile, string transformedResultFile, out string diffFile)
        {
            diffFile = Path.GetTempFileName();

            // ReSharper disable BitwiseOperatorOnEnumWithoutFlags
            var xmldiff = new XmlDiff(XmlDiffOptions.IgnoreDtd |
                                      XmlDiffOptions.IgnoreNamespaces |
                                      XmlDiffOptions.IgnorePrefixes |
                                      XmlDiffOptions.IgnoreWhitespace |
                                      XmlDiffOptions.IgnoreXmlDecl);

            // ReSharper restore BitwiseOperatorOnEnumWithoutFlags
            using (var writer = new XmlTextWriter(diffFile, Encoding.UTF8))
            {
                return(xmldiff.Compare(expectationFile, transformedResultFile, false, writer));
            }
        }
Example #24
0
        /// <summary>
        /// Invokes the XmlDiff Compare method. The strings attribute order and whitespace are ignored.
        /// </summary>
        /// <param name="diffs">The raw byte array of "diffgram" XML returned.</param>
        /// <param name="same">True if the two xml strings are identical.</param>
        /// <returns>Number of bytes read.</returns>
        private int Compare(out byte [] diffs, out bool same)
        {
            Encoding enc = Encoding.UTF8;             //.Unicode;

            System.IO.Stream stream = new System.IO.MemoryStream();
            XmlDiff          xDiff  = new XmlDiff(XmlDiffOptions.IgnoreWhitespace);
            XmlWriter        writer = new XmlTextWriter(stream, enc);

            same            = xDiff.Compare(m_baseReader, m_targetReader, writer);
            stream.Position = 0;
            diffs           = new Byte[stream.Length];
            int bytesRead = stream.Read(diffs, 0, (int)stream.Length);

            writer.Close();             // closes stream too.
            m_Compared = true;
            return(bytesRead);
        }
Example #25
0
        private void GenerateXmlDiffReport(string sourceXmlFile, string sourceXmlContent, string changedXmlFile, string changedXmlContent, string resultHtmlViewFile, XmlDiffOptions options)
        {
            MemoryStream  diffgram         = new MemoryStream();
            XmlTextWriter diffgramWriter   = new XmlTextWriter(new StreamWriter(diffgram));
            var           sourceXmlStream  = new MemoryStream(sourceXmlContent.ToByteArray());
            XmlReader     sourceXmlReader  = XmlReader.Create(new StreamReader(sourceXmlStream));
            XmlReader     changedXmlReader = XmlReader.Create(new StreamReader(new MemoryStream(changedXmlContent.ToByteArray())));

            logger.LogInfo("Comparing " + sourceXmlFile + " & " + changedXmlFile);
            XmlDiff xmlDiff    = new XmlDiff(options);
            bool    bIdentical = xmlDiff.Compare(sourceXmlReader, changedXmlReader, diffgramWriter);

            logger.LogInfo("Files compared " + (bIdentical ? "identical." : "different."));

            var        resultMS   = new MemoryStream();
            var        resultSW   = new StreamWriter(resultMS);
            TextWriter resultHtml = resultSW;

            //Wrapping
            resultHtml.Write("<html><style>td{ max-width:1000px; }</style><body><table>");

            diffgram.Seek(0, SeekOrigin.Begin);
            XmlDiffView xmlDiffView = new XmlDiffView();

            sourceXmlStream.Position = 0;
            XmlTextReader sourceReader = new XmlTextReader(sourceXmlStream);

            sourceReader.XmlResolver = null;
            xmlDiffView.Load(sourceReader, new XmlTextReader(diffgram));
            //This gets the differences but just has the
            //rows and columns of an HTML table
            xmlDiffView.SideBySideHtmlHeader(sourceXmlFile, changedXmlFile, bIdentical, resultHtml);
            xmlDiffView.GetHtml(resultHtml);
            xmlDiffView.GetHtml(resultHtml);

            resultHtml.WriteLine("</table></table></body></html>");
            using (var fs = new FileStream(resultHtmlViewFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                resultMS.WriteTo(fs);
            }
            resultSW.Close();
            resultMS.Close();
            resultHtml.Close();

            logger.LogInfo(resultHtmlViewFile + " saved successfully.");
        }
Example #26
0
        public static bool CompareXml(XDocument xDocOrigin, XDocument xDocNew, out string resultXml)
        {
            var resultCompare = false;
            var result        = new StringBuilder();

            using (var stringWriter = new StringWriter(result))
            {
                using (var xmlTextWriter = new XmlTextWriter(stringWriter))
                {
                    xmlTextWriter.Formatting = Formatting.Indented;
                    var xmlDiff = new XmlDiff
                    {
                        Options = XmlDiffOptions.IgnoreWhitespace | XmlDiffOptions.IgnoreComments |
                                  XmlDiffOptions.IgnoreXmlDecl
                    };

                    try
                    {
                        resultCompare = xmlDiff.Compare(xDocOrigin.CreateReader(), xDocNew.CreateReader(), xmlTextWriter);
                    }
                    catch (Exception e)
                    {
                        resultXml = "";
                        return(false);
                    }
                }
            }

            if (!resultCompare)
            {
                var xmlDiffView = new XmlDiffView();
                var diffGram    = new XmlTextReader(new StringReader(result.ToString()));
                xmlDiffView.Load(xDocOrigin.CreateReader(), diffGram);

                // orig.Close();
                diffGram.Close();

                resultXml = result.Replace("xd:", "").ToString();

                return(true);
            }

            resultXml = "";

            return(false);
        }
Example #27
0
        /// <summary>
        /// Compares two XML files to see if they are the same.
        /// </summary>
        /// <returns>
        /// Returns true if two XML files are functionally identical, ignoring comments, white space, and child order.
        /// </returns>
        /// <remarks>http://stackoverflow.com/a/19954063/4035</remarks>
        protected static bool XmlFilesAreIdentical(string file1, string file2)
        {
            var xmldiff = new XmlDiff();
            var r1 = XmlReader.Create(new StringReader(file1));
            var r2 = XmlReader.Create(new StringReader(file2));
            var sw = new StringWriter();
            var xw = new XmlTextWriter(sw) {Formatting = Formatting.Indented};

            xmldiff.Options = XmlDiffOptions.IgnorePI |
                              XmlDiffOptions.IgnoreChildOrder |
                              XmlDiffOptions.IgnoreComments |
                              XmlDiffOptions.IgnoreWhitespace;
            bool areIdentical = xmldiff.Compare(r1, r2, xw);

            string differences = sw.ToString();

            return areIdentical;
        }
        public void Example_5()
        {
            /*
             * Here's an example that uses XMLDiffPatch.
             * Now we're finally comparing xml :)
             * but the readability is... <insert adjective here> :/
             * And we're back to Assert.True :(
             */

            const string actualXml = @"<?xml version='1.0'?><albums><album title='Purple Train' /></albums>";
            var          diff      = new XmlDiff(XmlDiffOptions.None);

            using (var expected = XElement.Parse(XML).CreateReader())
                using (var actual = XElement.Parse(actualXml).CreateReader())
                {
                    Assert.True(diff.Compare(expected, actual));
                }
        }
        /// <summary>
        /// Create a diff between 2 xmls
        /// http://msdn.microsoft.com/en-us/library/aa302294.aspx
        /// </summary>
        /// <param name="xml"></param>
        /// <param name="xml2"></param>
        /// <returns></returns>
        private static string GetXmlDiff(string xml, string xml2)
        {
            using (var sw = new StringWriter())
            {
                using (XmlWriter diffgramWriter = new XmlTextWriter(sw))
                {
                    XmlDiff xmldiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder |
                                                  XmlDiffOptions.IgnoreNamespaces |
                                                  XmlDiffOptions.IgnorePrefixes);

                    XmlTextReader reader1 = new XmlTextReader(new StringReader(xml));
                    XmlTextReader reader2 = new XmlTextReader(new StringReader(xml2));

                    bool bIdentical = xmldiff.Compare(reader1, reader2, diffgramWriter);
                }
                return(sw.ToString());
            }
        }
Example #30
0
        public static bool CompareJson(string expected, string actual)
        {
            var expectedDoc = JsonConvert.DeserializeXmlNode(expected, "root");
            var actualDoc   = JsonConvert.DeserializeXmlNode(actual, "root");
            var diff        = new XmlDiff(XmlDiffOptions.IgnoreWhitespace |
                                          XmlDiffOptions.IgnoreChildOrder);

            using (var ms = new MemoryStream())
                using (var writer = new XmlTextWriter(ms, Encoding.UTF8))
                {
                    var result = diff.Compare(expectedDoc, actualDoc, writer);
                    if (!result)
                    {
                        ms.Seek(0, SeekOrigin.Begin);
                        Console.WriteLine(new StreamReader(ms).ReadToEnd());
                    }
                    return(result);
                }
        }
        public void CanConfigureNotToUseValidatingParser()
        {
            DiffConfiguration diffConfiguration = new DiffConfiguration(false);
            Assert.AreEqual(false, diffConfiguration.UseValidatingParser);

            Stream controlFileStream = ValidatorTests.ValidFile;
            Stream testFileStream = ValidatorTests.InvalidFile;
            try {
                XmlDiff diff = new XmlDiff(new XmlInput(controlFileStream),
                                           new XmlInput(testFileStream),
                                           diffConfiguration);
                diff.Compare();
            } catch (XmlSchemaException e) {
                Assert.Fail("Unexpected validation failure: " + e.Message);
            } finally {
                controlFileStream.Close();
                testFileStream.Close();
            }
        }
 private void PerformAssertion(XmlDiff diff, bool assertion)
 {
     Assert.AreEqual(assertion, diff.Compare().Equal);
     Assert.AreEqual(assertion, diff.Compare().Identical);
 }
        public void DefaultConfiguredToUseValidatingParser()
        {
            DiffConfiguration diffConfiguration = new DiffConfiguration();
            Assert.AreEqual(DiffConfiguration.DEFAULT_USE_VALIDATING_PARSER,
                                   diffConfiguration.UseValidatingParser);

            Stream controlFileStream = ValidatorTests.ValidFile;
            Stream testFileStream = ValidatorTests.InvalidFile;
            try {
                XmlDiff diff = new XmlDiff(new StreamReader(controlFileStream),
                                           new StreamReader(testFileStream));
                diff.Compare();
                Assert.Fail("Expected validation failure");
            } catch (XmlSchemaException e) {
                string message = e.Message; // to prevent 'unused variable' compiler warning
            } finally {
                controlFileStream.Close();
                testFileStream.Close();
            }
        }