internal static void Equal(string file1, string file2) { var diffFile = @"diff.xml"; XmlTextWriter tw = new XmlTextWriter(new StreamWriter(diffFile)); tw.Formatting = Formatting.Indented; var diff = new XmlDiff(); diff.Options = XmlDiffOptions.IgnoreWhitespace | XmlDiffOptions.IgnoreComments | XmlDiffOptions.IgnoreXmlDecl; var result = diff.Compare(file1, file2, false, tw); tw.Close(); if (!result) { //Files were not equal, so construct XmlDiffView. XmlDiffView dv = new XmlDiffView(); //Load the original file again and the diff file. XmlTextReader orig = new XmlTextReader(file1); XmlTextReader diffGram = new XmlTextReader(diffFile); dv.Load(orig, diffGram); string tempFile = "test.htm"; StreamWriter sw1 = new StreamWriter(tempFile); //Wrapping sw1.Write("<html><body><table>"); sw1.Write("<tr><td><b>"); sw1.Write(file1); sw1.Write("</b></td><td><b>"); sw1.Write(file2); sw1.Write("</b></td></tr>"); //This gets the differences but just has the //rows and columns of an HTML table dv.GetHtml(sw1); //Finish wrapping up the generated HTML and //complete the file by putting legend in the end just like the //online tool. sw1.Write("<tr><td><b>Legend:</b> <font style='background-color: yellow'" + " color='black'>added</font> <font style='background-color: red'" + "color='black'>removed</font> <font style='background-color: " + "lightgreen' color='black'>changed</font> " + "<font style='background-color: red' color='blue'>moved from</font>" + " <font style='background-color: yellow' color='blue'>moved to" + "</font> <font style='background-color: white' color='#AAAAAA'>" + "ignored</font></td></tr>"); sw1.Write("</table></body></html>"); //HouseKeeping...close everything we dont want to lock. sw1.Close(); orig.Close(); diffGram.Close(); Process.Start("test.htm"); } Assert.True(result); }
/// <summary> /// compares two xml data sources as xml strings /// </summary> /// <param name="querySource">the SQL query text for the source</param> /// <param name="queryTarget">the SQL query text for the target</param> /// <param name="connectionSource">The sql connection object for the source</param> /// <param name="connectionTarget">The sql connection object for the target</param> /// <param name="asTextFile"></param> /// <returns></returns> public static string CompareData(string querySource, string queryTarget, SqlConnection connectionSource, SqlConnection connectionTarget, bool asTextFile) { bool isEqual = false; string tempFile = "TableDiffReport.html"; string sourceName = querySource.Replace("select * from ", "").Split(' ')[0].Replace("\\", "_").Replace(":", "-") + ".xml"; string targetName = queryTarget.Replace("select * from ", "").Split(' ')[0].Replace("\\", "_").Replace(":", "-") + ".xml"; //output diff file. string diffFile = sourceName.Replace(".xml", "") + "_DIFF_" + targetName; XmlDiffOptions xdo = XmlDiffOptions.IgnoreWhitespace | XmlDiffOptions.IgnoreComments | XmlDiffOptions.IgnoreChildOrder | XmlDiffOptions.IgnoreNamespaces | XmlDiffOptions.IgnorePI; XmlDocument original = new XmlDocument(); original.LoadXml(GetXMLData(querySource, connectionSource)); original.Save(sourceName); XmlDocument doc = new XmlDocument(); doc.LoadXml(GetXMLData(queryTarget, connectionTarget)); doc.Save(targetName); if (asTextFile) { XmlDiffView diffView = new XmlDiffView(); diffView.DifferencesAsFormattedText(sourceName, targetName, diffFile.Replace(".xml", "") + ".txt", false, xdo); diffView = null; return(diffFile.Replace(".xml", "") + ".txt"); } else { XmlTextWriter diffWriter = new XmlTextWriter(diffFile, Encoding.UTF8); diffWriter.Formatting = Formatting.Indented; using (diffWriter) { XmlDiff diff = new XmlDiff(); isEqual = diff.Compare(original, doc, diffWriter); diff.Options = xdo; } if (isEqual) { //This means the files were identical for given options. MessageBox.Show("Tables are identical", "Identical", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(string.Empty); } using (XmlReader diffGram = XmlReader.Create(diffFile)) { XmlDiffView diffView = new XmlDiffView(); diffView.Load(new XmlNodeReader(original), diffGram); using (TextWriter htmlWriter = new StreamWriter(tempFile)) { SideBySideXmlNotepadHeader(sourceName, targetName, htmlWriter); diffView.GetHtml(htmlWriter); } diffView = null; } } return(tempFile); }
} //OK MPS - 04/11/2014 public bool XmlDiffHtm(string source, string target, string intervalo, string server, string classe) { // Randomiza para criar arquivos unicos de comparação r = new Random(); bool isEqual = false; //Pega o usuário logado... MembershipUser currentUser = Membership.GetUser(); //output diff file. startupPath = ConfigurationManager.AppSettings["XMLData"] + "\\RPTs"; diffFile = startupPath + Path.DirectorySeparatorChar + "vxd" + r.Next() + ".out"; // XmlTextWriter tw = new XmlTextWriter(new StreamWriter(diffFile)); tw.Formatting = Formatting.Indented; try { XmlReader expected = XmlReader.Create(new StringReader(target)); XmlReader actual = XmlReader.Create(new StringReader(source)); XmlDiff diff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder | XmlDiffOptions.IgnoreComments | XmlDiffOptions.IgnoreXmlDecl | XmlDiffOptions.IgnoreWhitespace); isEqual = diff.Compare(actual, expected, tw); tw.Close(); //----------------------------------------------------------------- // Cria a comparação dos XMLs... XmlDiffView dv = new XmlDiffView(); // Carrega o arquivo orig = source + o arquivo XML das Diff... XmlTextReader orig = new XmlTextReader(new StringReader(source)); //source XmlTextReader diffGram = new XmlTextReader(diffFile); dv.Load(orig, diffGram); orig.Close(); diffGram.Close(); // Chama a função para gravar e formatar o conteudo + diff em HTM... if (!isEqual) { GrHtm(dv, intervalo, server, classe); } // return(isEqual); } catch (XmlException xe) { divMessage.InnerHtml = "<span id='msg' style='color:#FF3300;font-size:Smaller;font-weight:bold;'>Ocorreu um erro de Comparação - " + xe.StackTrace + "</span>"; return(isEqual); } } //OK MPS - 04/11/2014
private static string GetHtmlDiff(string sourceXml, string diffgram) { var diffView = new XmlDiffView(); diffView.Load( XmlReader.Create(new StringReader(sourceXml)), XmlReader.Create(new StringReader(diffgram))); var builder = new StringBuilder(); builder.Append("<html><body><table>"); diffView.GetHtml(new StringWriter(builder)); builder.Append("</table></body></html>"); return(builder.ToString()); }
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); }
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."); }
private string CreateDiffAsHtml(string expectationFile, string diffFile) { var xmlDiffView = new XmlDiffView(); using (var expectationFileReader = new XmlTextReader(expectationFile)) using (var diffFileReader = new XmlTextReader(diffFile)) { xmlDiffView.Load(expectationFileReader, diffFileReader); } var htmlContent = new StringBuilder(); var xmlDiffWriter = new StringWriter(htmlContent); xmlDiffWriter.Write("<html><body><table width='100%'><tr><th>Expected</th><th>Actual</th></tr>"); xmlDiffView.GetHtml(xmlDiffWriter); xmlDiffWriter.Write("</table></body></html>"); return(htmlContent.ToString()); }
/// <summary> /// Asserts the values are equal. /// </summary> /// <param name="expectedXmlFilePath">The expected XML file path.</param> /// <param name="actualXmlFilePath">The actual XML file path.</param> /// <param name="testContext">The test context.</param> /// <param name="message">The message.</param> public static void AreEqual(string expectedXmlFilePath, string actualXmlFilePath, TestContext testContext, string message = "") { using (TempFile diffGramFile = new TempFile(".xml")) { bool identical; using (XmlWriter diffGramWriter = XmlWriter.Create(diffGramFile.FilePath)) { XmlDiff xmldiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder | XmlDiffOptions.IgnoreNamespaces | XmlDiffOptions.IgnorePrefixes); identical = xmldiff.Compare(expectedXmlFilePath, actualXmlFilePath, false, diffGramWriter); } if (!identical) { // generate visual diff using (TempHtmlFile diffGramHtmlFile = new TempHtmlFile(null, testContext)) { XmlDiffView xmlDiffView = new XmlDiffView(); using (XmlTextReader sourceXml = new XmlTextReader(expectedXmlFilePath)) { using (XmlTextReader diffGram = new XmlTextReader(diffGramFile.FilePath)) { xmlDiffView.Load(sourceXml, diffGram); using (StreamWriter streamWriter = new StreamWriter(diffGramHtmlFile.FilePath)) { WriteHeaderForXmlDiffGram(expectedXmlFilePath, diffGramFile.FilePath, streamWriter); xmlDiffView.GetHtml(streamWriter); streamWriter.Write("</body></html>"); } } } message += " Diff: " + diffGramHtmlFile.FileName; } } Assert.IsTrue(identical, message); } }
public void DoCompare(string file1, string file2) { Random r = new Random(); //to randomize the output files and hence allow //us to generate multiple files for the same pair //of comparisons. string tempPath = Path.GetTempPath(); //output diff file. diffFile = tempPath + Path.DirectorySeparatorChar + "vxd.out"; XmlTextWriter tw = new XmlTextWriter(new StreamWriter(diffFile)); tw.Formatting = Formatting.Indented; //This method sets the diff.Options property. SetDiffOptions(); bool isEqual = false; //Now compare the two files. try { isEqual = diff.Compare(file1, file2, compareFragments, tw); } catch (XmlException xe) { MessageBox.Show("An exception occured while comparing\n" + xe.StackTrace); } finally { tw.Close(); } if (isEqual) { //This means the files were identical for given options. MessageBox.Show("Files Identical for the given options"); return; //dont need to show the differences. } //Files were not equal, so construct XmlDiffView. XmlDiffView dv = new XmlDiffView(); //Load the original file again and the diff file. XmlTextReader orig = new XmlTextReader(file1); XmlTextReader diffGram = new XmlTextReader(diffFile); dv.Load(orig, diffGram); //Wrap the HTML file with necessary html and //body tags and prepare it before passing it to the GetHtml method. string tempFile = tempPath + "diff" + r.Next() + ".htm"; StreamWriter sw1 = new StreamWriter(tempFile); sw1.Write("<html><body><table width='100%'>"); //Write Legend. sw1.Write("<tr><td colspan='2' align='center'><b>Legend:</b> <font style='background-color: yellow'" + " color='black'>added</font> <font style='background-color: red'" + " color='black'>removed</font> <font style='background-color: " + "lightgreen' color='black'>changed</font> " + "<font style='background-color: red' color='blue'>moved from</font>" + " <font style='background-color: yellow' color='blue'>moved to" + "</font> <font style='background-color: white' color='#AAAAAA'>" + "ignored</font></td></tr>"); sw1.Write("<tr><td><b> File Name : "); sw1.Write(textBox1.Text); sw1.Write("</b></td><td><b> File Name : "); sw1.Write(textBox2.Text); sw1.Write("</b></td></tr>"); //This gets the differences but just has the //rows and columns of an HTML table dv.GetHtml(sw1); //Finish wrapping up the generated HTML and complete the file. sw1.Write("</table></body></html>"); //HouseKeeping...close everything we dont want to lock. sw1.Close(); dv = null; orig.Close(); diffGram.Close(); File.Delete(diffFile); //Open the IE Control window and pass it the HTML file we created. Browser b = new Browser(tempFile); b.Show(); //Display it! //Done! }
private void DeserializeGeneric <T>(string inputUBLfile, string outputFile, XmlDiff xmldiff) { string contentUBL = File.ReadAllText(inputUBLfile); dynamic retvalue = null; try { retvalue = Assert.XmlDeserialize <T>(contentUBL); } catch (Exception e) { throw e; } var document = RemoveEmptyNodes(retvalue.Serialize()); document.Save(outputFile); //Not possible to set others XmlDiffOptions //Assert.Xml.AreEqual(contentUBL, document.ToString(), XmlOptions.Loose); StringBuilder output = new StringBuilder(); XmlWriter diffgramWriter = XmlWriter.Create(output); bool bIdentical = xmldiff.Compare(inputUBLfile, outputFile, true, diffgramWriter); diffgramWriter.Close(); //********* realizzazione report html in cartella di Output con evidenze visive colorate string diffFile = @"..\..\OutputXML\" + Guid.NewGuid() + ".out"; XmlTextWriter twriter = new XmlTextWriter(new StreamWriter(diffFile)); twriter.Formatting = Formatting.Indented; bool bIdentical2 = xmldiff.Compare(inputUBLfile, outputFile, true, twriter); twriter.Close(); string statusreport = (bIdentical.Equals(true)) ? "Identical Xml Files" : "Different Xml Files"; //Different files: XmlDiffView. XmlDiffView diffview = new XmlDiffView(); //Load the original file again and the diff file. XmlTextReader originFile = new XmlTextReader(inputUBLfile); XmlTextReader diffGram = new XmlTextReader(diffFile); diffview.Load(originFile, diffGram); //Wrap HTML file string tempFile = @"..\..\OutputXML\" + "diff_" + Guid.NewGuid() + ".htm"; StreamWriter swriter = new StreamWriter(tempFile); swriter.Write("<html><body><table width='100%'>"); // Legend. swriter.Write("<tr><td colspan='2' align='center'><h1>XML Comparison Report: " + statusreport + "</h1><\br/><b>Legend:</b> <font style='background-color: yellow'" + " color='black'>added</font> <font style='background-color: red'" + " color='black'>removed</font> <font style='background-color: " + "lightgreen' color='black'>changed</font> " + "<font style='background-color: red' color='blue'>moved from</font>" + " <font style='background-color: yellow' color='blue'>moved to" + "</font> <font style='background-color: white' color='#AAAAAA'>" + "ignored</font></td></tr>"); swriter.Write("<tr><td><b> File Name : "); swriter.Write(inputUBLfile); swriter.Write("</b></td><td><b> File Name : "); swriter.Write(outputFile); swriter.Write("</b></td></tr>"); //Difference in HTML table diffview.GetHtml(swriter); //conmplete file HTML swriter.Write("</table></body></html>"); swriter.Close(); diffview = null; originFile.Close(); diffGram.Close(); File.Delete(diffFile); if (bIdentical) { Console.WriteLine(String.Format("Identical XML File \n\n Src file: {0} \n Trgt file: {1}", inputUBLfile, outputFile)); Assert.IsTrue(bIdentical, "XML Deserialized comparison: {0}", output); } else { Console.WriteLine("HTML report difference: " + tempFile); Assert.Fail("XML Deserialized NOT EQUAL! Below comparison details:\n\n {0}", output); } }
public void DoCompare(string file1, string file2) { try { MainProBar.Visible = true; MainProBar.Minimum = 1; MainProBar.Maximum = 18; MainProBar.Value = 6; MainProBar.Step = 1; MainProBar.PerformStep(); //Convert HL7 to XML System.IO.StreamReader InputFile = new System.IO.StreamReader(file1); string InputHL7 = InputFile.ReadToEnd(); InputFile.Close(); InputHL7 = InputHL7.Replace((Char)127, (Char)13); String InputHL7asXml = HL7ToXmlConverter.ConvertToXml(InputHL7, ""); string myTempFile1 = Path.Combine(Path.GetTempPath(), "Input.xml"); System.IO.StreamWriter fileI = new System.IO.StreamWriter(myTempFile1); fileI.WriteLine(InputHL7asXml); fileI.Close(); file1 = myTempFile1; MainProBar.PerformStep(); System.IO.StreamReader OutputFile = new System.IO.StreamReader(file2); string OutputHL7 = OutputFile.ReadToEnd(); OutputFile.Close(); OutputHL7 = OutputHL7.Replace((Char)127, (Char)13); String OutputHL7asXml = HL7ToXmlConverter.ConvertToXml(OutputHL7, ""); string myTempFile2 = Path.Combine(Path.GetTempPath(), "Output.xml"); System.IO.StreamWriter fileO = new System.IO.StreamWriter(myTempFile2); fileO.WriteLine(OutputHL7asXml); fileO.Close(); file2 = myTempFile2; MainProBar.PerformStep(); string startupPath = Application.StartupPath; //output diff file. diffFile = startupPath + Path.DirectorySeparatorChar + "vxd.out"; XmlTextWriter tw = new XmlTextWriter(new StreamWriter(diffFile)); tw.Formatting = Formatting.Indented; MainProBar.PerformStep(); //This method sets the diff.Options property. SetDiffOptions(); bool isEqual = false; //Now compare the two files. try { isEqual = diff.Compare(file1, file2, compareFragments, tw); } catch (XmlException xe) { xe.StackTrace.ToString(); //MessageBox.Show("An exception occured while comparing\n" + xe.StackTrace); } finally { tw.Close(); } MainProBar.PerformStep(); if (isEqual) { //This means the files were identical for given options. MessageBox.Show("Files Identical for the given options"); MainProBar.Value = 1; return; //dont need to show the differences. } MainProBar.PerformStep(); //Files were not equal, so construct XmlDiffView. XmlDiffView dv = new XmlDiffView(); //Load the original file again and the diff file. XmlTextReader orig = new XmlTextReader(file1); XmlTextReader diffGram = new XmlTextReader(diffFile); dv.Load(orig, diffGram); MainProBar.PerformStep(); //Wrap the HTML file with necessary html and //body tags and prepare it before passing it to the GetHtml method. string tempFile = startupPath + Path.DirectorySeparatorChar + "Compare_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm"; StreamWriter sw1 = new StreamWriter(tempFile); MainProBar.PerformStep(); sw1.Write("<html><body><table width='100%'>"); //Write Legend. sw1.Write("<tr><td colspan='2' align='center'><b>Legend:</b> <font style='background-color: yellow'" + " color='black'>added</font> <font style='background-color: red'" + " color='black'>removed</font> <font style='background-color: " + "lightgreen' color='black'>changed</font> " + "<font style='background-color: red' color='blue'>moved from</font>" + " <font style='background-color: yellow' color='blue'>moved to" + "</font> <font style='background-color: white' color='#AAAAAA'>" + "ignored</font></td></tr>"); sw1.Write("<tr><td><b> File Name : "); sw1.Write(OriginalPath.Text); sw1.Write("</b></td><td><b> File Name : "); sw1.Write(ComparePath.Text); sw1.Write("</b></td></tr>"); MainProBar.PerformStep(); //This gets the differences but just has the //rows and columns of an HTML table dv.GetHtml(sw1); MainProBar.PerformStep(); //Finish wrapping up the generated HTML and complete the file. sw1.Write("</table></body></html>"); //Open the IE Control window and pass it the HTML file we created. ReportWindow.Navigate(new Uri(tempFile)); MainProBar.PerformStep(); //HouseKeeping...close everything we dont want to lock. dv = null; file1 = null; file2 = null; sw1.Close(); orig.Close(); diffGram.Close(); orig.Dispose(); diffGram.Dispose(); tw.Dispose(); sw1.Dispose(); fileI.Dispose(); fileO.Dispose(); File.Delete(diffFile); GC.Collect(); MainProBar.PerformStep(); MainProBar.PerformStep(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); MainProBar.Value = 1; } }
/// <summary> /// Compare resopnse from server with ethalon /// </summary> /// <param name="responseFile">Ethalon response file</param> /// <param name="responseFileReal">Real response file</param> /// <param name="resultMessage">Result mesage</param> /// <param name="pathToCompareFile">Path to file with comparation</param> /// <returns>Return true if file identical and false in other case</returns> private bool CompareResponse(string responseFile, string responseFileReal, out string resultMessage, ref string pathToCompareFile) { bool result = false; string diffFile = null; XmlDiff diff = new XmlDiff(); XmlTextReader orig = null; XmlDiffOptions diffOptions = new XmlDiffOptions(); StreamWriter sw1 = null; XmlTextReader diffGram = null; //output diff file. diffFile = Path.GetDirectoryName(responseFileReal) + "\\vxd.out"; XmlTextWriter tw = new XmlTextWriter(new StreamWriter(diffFile)); tw.Formatting = Formatting.Indented; //This method sets the diff.Options property. diffOptions = XmlDiffOptions.None; diff.Options = diffOptions; bool isEqual = false; //Now compare the two files. try { isEqual = diff.Compare(responseFile, responseFileReal, false, tw); } catch (XmlException ex) { pathToCompareFile = ""; resultMessage = ex.Message; result = false; return(result); } finally { tw.Close(); } if (isEqual) { //This means the files were identical for given options. result = true; pathToCompareFile = ""; resultMessage = "Files are equal."; return(result); //dont need to show the differences. } //Files were not equal, so construct XmlDiffView. XmlDiffView dv = new XmlDiffView(); result = false; resultMessage = "Files are not equal."; try { //Load the original file again and the diff file. orig = new XmlTextReader(responseFile); diffGram = new XmlTextReader(diffFile); dv.Load(orig, diffGram); //Wrap the HTML file with necessary html and //body tags and prepare it before passing it to the GetHtml method. sw1 = new StreamWriter(pathToCompareFile); sw1.Write("<html><body><table width='100%'>"); //Write Legend. sw1.Write("<tr><td colspan='2' align='center'><b>Legend:</b> <font style='background-color: yellow'" + " color='black'>added</font> <font style='background-color: red'" + " color='black'>removed</font> <font style='background-color: " + "lightgreen' color='black'>changed</font> " + "<font style='background-color: red' color='blue'>moved from</font>" + " <font style='background-color: yellow' color='blue'>moved to" + "</font> <font style='background-color: white' color='#AAAAAA'>" + "ignored</font></td></tr>"); sw1.Write("<tr><td><b> File Name : "); sw1.Write(responseFile); sw1.Write("</b></td><td><b> File Name : "); sw1.Write(responseFileReal); sw1.Write("</b></td></tr>"); //This gets the differences but just has the //rows and columns of an HTML table dv.GetHtml(sw1); //Finish wrapping up the generated HTML and complete the file. sw1.Write("</table></body></html>"); } catch (Exception ex) { resultMessage = ex.Message; result = false; return(result); //dont need to show the differences. } finally { //HouseKeeping...close everything we dont want to lock. if (sw1 != null) { sw1.Close(); } ; dv = null; if (orig != null) { orig.Close(); } ; if (diffGram != null) { diffGram.Close(); } ; } resultMessage = "File are not equal."; return(result); }
public ActionResult Compare() { string file2 = "C:/Users/Rym/Documents/XMLEditor/XMLEditor/XMLEditor/fichier2.xml"; string ch = " xml:space='preserve'"; string xml_data = (string)Session["xml_selected"]; while (xml_data.Contains(ch)) { xml_data = xml_data.Remove(xml_data.IndexOf(ch), ch.Length); } XmlDocument doc2 = new XmlDocument(); doc2.LoadXml((string)Session["xml_selected"]); doc2.Save(file2); string file1 = "C:/Users/Rym/Documents/XMLEditor/XMLEditor/XMLEditor/fichier_xml/" + Session["file_name"]; string diff = "C:/Users/Rym/Documents/XMLEditor/XMLEditor/XMLEditor/Compare.xml"; bool isEqual = DoCompare(file2, file1, diff); if (isEqual) { System.Diagnostics.Debug.WriteLine("Files Identical for the given options"); return(PartialView("Equal")); } //Files were not equal, so construct XmlDiffView. XmlDiffView dv = new XmlDiffView(); //Load the original file again and the diff file. XmlTextReader orig = new XmlTextReader(file2); XmlTextReader diffGram = new XmlTextReader(diff); dv.Load(orig, diffGram); //Wrap the HTML file with necessary html and //body tags and prepare it before passing it to the GetHtml method. string tempFile = "C:/Users/Rym/Documents/XMLEditor/XMLEditor/XMLEditor/Views/Home/Compare.cshtml"; StreamWriter sw1 = new StreamWriter(tempFile); sw1.Write("<style>td,th{ border:none;} .cadre {background-color: #1D1B1B;border-radius: 25px; padding:25px;} .my-custom-scrollbar {position: relative;height: 350px; overflow: auto;} .table-wrapper-scroll-y {display: block;} .table{ background:white; width:100%}</style><div class=\"cadre\"><div class=\"table-wrapper-scroll-y my-custom-scrollbar\"><table class=\"table\" > "); //Write Legend. sw1.Write("<tr><td colspan='2' align='center'><b>Legend:</b> <font style='background-color: yellow'" + " color='black'>added</font> <font style='background-color: red'" + " color='black'>removed</font> <font style='background-color: " + "lightgreen' color='black'>changed</font> " + "<font style='background-color: red' color='blue'>moved from</font>" + " <font style='background-color: yellow' color='blue'>moved to" + "</font> <font style='background-color: white' color='#AAAAAA'>" + "ignored</font></td></tr>"); sw1.Write("<tr><td><b>Choosen Version"); sw1.Write("</b></td><td><b>Current Version"); sw1.Write("</b></td></tr>"); //This gets the differences but just has the //rows and columns of an HTML table dv.GetHtml(sw1); //Finish wrapping up the generated HTML and complete the file. sw1.Write("</table></div></div>"); //HouseKeeping...close everything we dont want to lock. sw1.Close(); dv = null; orig.Close(); diffGram.Close(); //System.IO.File.Delete(diffile); return(PartialView("Compare")); }
public static string CompareXML(string SourceXMLContent, string TargetXMLContent, out bool isEqual) { var diffdir = AppDomain.CurrentDomain.BaseDirectory; Random r = new Random(); var difffile = Path.Combine(diffdir, r.Next() + ".txt"); XmlDiffView dv = new XmlDiffView(); StringReader sourceRdr = new StringReader(SourceXMLContent); XmlReader sourcexmlreader = XmlReader.Create(sourceRdr); StringReader targetRdr = new StringReader(TargetXMLContent); XmlReader targetxmlreader = XmlReader.Create(targetRdr); try { using (XmlTextWriter tw = new XmlTextWriter(new StreamWriter(difffile)) { Formatting = Formatting.Indented }) { XmlDiff diffnew = new XmlDiff() { Options = new XmlDiffOptions(), Algorithm = XmlDiffAlgorithm.Auto }; isEqual = diffnew.Compare(sourcexmlreader, targetxmlreader, tw); } using (StringReader sourceRdr1 = new StringReader(SourceXMLContent)) { using (XmlTextReader orig = new XmlTextReader(sourceRdr1)) { using (XmlTextReader diffGram = new XmlTextReader(difffile)) { dv.Load(orig, diffGram); } File.Delete(difffile); } } } catch (Exception ex) { isEqual = false; string msg = ex.Message; } finally { sourcexmlreader.Close(); targetxmlreader.Close(); sourceRdr.Close(); targetRdr.Close(); } StringBuilder sb = new StringBuilder(""); sb.Append("<html><body><table width='100%'>"); //Write Legend. sb.Append("<tr><td colspan='2' align='center'><b>Legend:</b>"); sb.Append("<font style='background-color: yellow' color='black'>added</font> "); sb.Append("<font style='background-color: red' color='black'>removed</font> "); sb.Append("<font style='background-color: lightgreen' color='black'>changed</font> "); sb.Append("<font style='background-color: red' color='blue'>moved from</font> "); sb.Append("<font style='background-color: yellow' color='blue'>moved to</font> "); sb.Append("<font style='background-color: white' color='#AAAAAA'>ignored</font></td></tr>"); sb.Append("<tr><td><b> Source XML Content : "); sb.Append("</b></td><td><b> Target XML Content : "); sb.Append("</b></td></tr>"); string tempFile = Path.Combine(diffdir, r.Next() + ".htm"); using (StreamWriter sw1 = new StreamWriter(tempFile)) { sw1.Write(sb.ToString()); dv.GetHtml(sw1); sw1.Write("</table></body></html>"); sw1.Close(); } string result = ""; using (var sr = new System.IO.StreamReader(tempFile)) { result = sr.ReadToEnd(); } File.Delete(tempFile); return(result); }
public void GenerateOutput(BuildItem buildItem, Stream outputStream, IDictionary <string, Stream> inputFormatStreams, string defaultNamespace) { outputStream = new UncloseableStream(outputStream); Stream undeadOial = inputFormatStreams["UndeadOIAL"]; Stream liveOial = inputFormatStreams["LiveOIAL"]; XmlDiff xmlDiff = new XmlDiff(XmlDiffOptions.IgnoreComments | XmlDiffOptions.IgnoreXmlDecl | XmlDiffOptions.IgnorePrefixes); xmlDiff.Algorithm = XmlDiffAlgorithm.Precise; bool identical = false; MemoryStream diffgram = new MemoryStream(8192); using (XmlWriter diffgramWriter = XmlWriter.Create(diffgram)) { try { using (XmlReader undeadReader = XmlReader.Create(undeadOial, XmlReaderSettings), liveReader = XmlReader.Create(liveOial, XmlReaderSettings)) { identical = xmlDiff.Compare(undeadReader, liveReader, diffgramWriter); } } finally { undeadOial.Seek(0, SeekOrigin.Begin); liveOial.Seek(0, SeekOrigin.Begin); } } // Files have been compared, and the diff has been written to the diffgramwriter. TextWriter resultHtml = new StreamWriter(outputStream); resultHtml.WriteLine("<html><head>"); resultHtml.WriteLine("<style TYPE='text/css' MEDIA='screen'>"); resultHtml.Write("<!-- td { font-family: Courier New; font-size:14; } " + "th { font-family: Arial; } " + "p { font-family: Arial; } -->"); resultHtml.WriteLine("</style></head>"); resultHtml.WriteLine("<body><h3 style='font-family:Arial'>XmlDiff view</h3><table border='0'><tr><td><table border='0'>"); resultHtml.WriteLine("<tr><th>Undead OIAL</th><th>Live OIAL</th></tr>" + "<tr><td colspan=2><hr size=1></td></tr>"); if (identical) { resultHtml.WriteLine("<tr><td colspan='2' align='middle'>Files are identical.</td></tr>"); } else { resultHtml.WriteLine("<tr><td colspan='2' align='middle'>Files are different.</td></tr>"); } diffgram.Seek(0, SeekOrigin.Begin); XmlDiffView xmlDiffView = new XmlDiffView(); XmlTextReader sourceReader = new XmlTextReader(undeadOial); sourceReader.XmlResolver = null; xmlDiffView.Load(sourceReader, new XmlTextReader(diffgram)); xmlDiffView.GetHtml(resultHtml); resultHtml.WriteLine("</table></table></body></html>"); resultHtml.Flush(); resultHtml.Close(); }