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); }
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 void GrHtm(XmlDiffView dv, string Dts, string Srv, string Cls) { string msg; StringBuilder msgHtm = new StringBuilder(); // HTMFile = startupPath + Path.DirectorySeparatorChar + Dts + "_" + Cls + "_" + Srv + "_diff" + r.Next() + ".htm"; StreamWriter sw1 = new StreamWriter(HTMFile); // Escreve o HTML com as diferenças para analise do LeHtm... // Cabecalho msg = "<html><body><table width='100%'>"; msgHtm.Append(msg); sw1.Write(msg); // Legenda msg = "<tr><td colspan='2' align='center'><b>" + Cls + "_" + Srv + " - Legenda:</b> <font style='background-color: yellow'" + " color='black'>adicionado</font> <font style='background-color: red'" + " color='black'>removido</font> <font style='background-color: " + "lightgreen' color='black'>alterado</font> " + "<font style='background-color: red' color='blue'>movido Ini</font>" + " <font style='background-color: yellow' color='blue'>movido Fim" + "</font> <font style='background-color: white' color='#AAAAAA'>" + "ignorado</font></td></tr>"; msgHtm.Append(msg); sw1.Write(msg); // Arquivos comparados... msg = "<tr><td><b> File Name : "; msgHtm.Append(msg); sw1.Write(msg); // msg = Dts.Substring(1,11); msgHtm.Append(msg); sw1.Write(msg); // msg = "</b></td><td><b> File Name : "; msgHtm.Append(msg); sw1.Write(msg); // msg = Dts.Substring(13,11); msgHtm.Append(msg); sw1.Write(msg); // msg = "</b></td></tr>"; msgHtm.Append(msg); sw1.Write(msg); // This gets the differences but just has the //rows and columns of an HTML table dv.GetHtml(sw1); // Finaliza o HTML msg = "</table></body></html>"; msgHtm.Append(msg); sw1.Write(msg); // Fecha arquivos sw1.Close(); dv = null; }
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(); }
private void DoCompare(string changed) { CleanupTempFiles(); XmlDocument original = this.model.Document; XmlDocument doc = new XmlDocument(); XmlReaderSettings settings = model.GetReaderSettings(); using (XmlReader reader = XmlReader.Create(changed, settings)) { doc.Load(reader); } string startupPath = Application.StartupPath; //output diff file. string diffFile = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".xml"); this.tempFiles.AddFile(diffFile, false); bool isEqual = false; 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 = XmlDiffOptions.None; } if (isEqual) { //This means the files were identical for given options. MessageBox.Show(this, SR.FilesAreIdenticalPrompt, SR.FilesAreIdenticalCaption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } string tempFile = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".htm"); tempFiles.AddFile(tempFile, false); using (XmlReader diffGram = XmlReader.Create(diffFile, settings)) { XmlDiffView diffView = new XmlDiffView(); diffView.Load(new XmlNodeReader(original), diffGram); using (TextWriter htmlWriter = new StreamWriter(tempFile)) { SideBySideXmlNotepadHeader(this.model.FileName, changed, htmlWriter); diffView.GetHtml(htmlWriter); } } /* Uri uri = new Uri(tempFile); WebBrowserForm browserForm = new WebBrowserForm(uri, "XmlDiff"); browserForm.Show(); */ }
static void Main(string[] args) { try { int curArgIndex = 0; bool bFragment = false; // decode options XmlDiffOptions options = XmlDiffOptions.None; string optionsString = string.Empty; while (curArgIndex < args.Length && (args[curArgIndex][0] == '/' || args[curArgIndex][0] == '-')) { if (args[curArgIndex].Length != 2) { System.Console.Write("Invalid option: " + args[curArgIndex] + "\n"); return; } switch (args[curArgIndex][1]) { case '?': WriteUsage(); return; case 'o': options |= XmlDiffOptions.IgnoreChildOrder; break; case 'c': options |= XmlDiffOptions.IgnoreComments; break; case 'p': options |= XmlDiffOptions.IgnorePI; break; case 'w': options |= XmlDiffOptions.IgnoreWhitespace; break; case 'n': options |= XmlDiffOptions.IgnoreNamespaces; break; case 'r': options |= XmlDiffOptions.IgnorePrefixes; break; case 'x': options |= XmlDiffOptions.IgnoreXmlDecl; break; case 'd': options |= XmlDiffOptions.IgnoreDtd; break; case 'f': bFragment = true; break; default: System.Console.Write("Invalid option: " + args[curArgIndex] + "\n"); return; } optionsString += args[curArgIndex][1]; curArgIndex++; } if (args.Length - curArgIndex < 3) { WriteUsage(); return; } bool includeIngnore = (args.Length - curArgIndex > 3); string sourceXmlFile = args[curArgIndex]; string changedXmlFile = args[curArgIndex + 1]; string resultHtmlViewFile = args[curArgIndex + 2]; string diffgramFile = args[curArgIndex + 3]; string ignoreFile = includeIngnore ? args[curArgIndex + 4] : null; MemoryStream diffgram = new MemoryStream(); XmlTextWriter diffgramWriter = new XmlTextWriter(new StreamWriter(diffgram)); var msg = "Comparing " + sourceXmlFile + " to " + changedXmlFile + " using ignore config " + ignoreFile; log.Info(msg); Console.WriteLine(msg); XmlDiff xmlDiff = new XmlDiff(options); bool bIdentical = true; try { bIdentical = xmlDiff.Compare(sourceXmlFile, changedXmlFile, bFragment, diffgramWriter, ignoreFile); } catch (Exception e) { log.Error(e.Message, e); Console.WriteLine(e.Message); return; } msg = "Files compared " + (bIdentical ? "identical." : "different."); log.Info(msg); Console.WriteLine(msg); msg = "Generating " + resultHtmlViewFile + " view file"; log.Info(msg); Console.WriteLine(msg); TextWriter resultHtml = new StreamWriter(new FileStream(resultHtmlViewFile, FileMode.Create, FileAccess.Write)); 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>" + sourceXmlFile + "</th><th>" + changedXmlFile + "</th></tr>" + "<tr><td colspan=2><hr size=1></td></tr>"); if (bIdentical) { 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); Microsoft.XmlDiffPatch.XmlDiffView xmlDiffView = new Microsoft.XmlDiffPatch.XmlDiffView(); XmlTextReader sourceReader; if (bFragment) { NameTable nt = new NameTable(); sourceReader = new XmlTextReader(new FileStream(sourceXmlFile, FileMode.Open, FileAccess.Read), XmlNodeType.Element, new XmlParserContext(nt, new XmlNamespaceManager(nt), string.Empty, XmlSpace.Default)); } else { sourceReader = new XmlTextReader(sourceXmlFile); } sourceReader.XmlResolver = null; xmlDiffView.Load(sourceReader, new XmlTextReader(diffgram)); xmlDiffView.GetHtml(resultHtml); resultHtml.WriteLine("</table></table></body></html>"); resultHtml.Close(); msg = resultHtmlViewFile + " saved successfully."; log.Info(msg); Console.WriteLine(msg); msg = "saving diffgram file"; log.Info(msg); Console.WriteLine(msg); using (FileStream file = new FileStream(diffgramFile, FileMode.Create, FileAccess.Write)) { diffgram.WriteTo(file); } msg = "diffgram file saved to " + diffgramFile; log.Info(msg); Console.WriteLine(msg); } catch (Exception e) { log.Error(e.Message, e); Console.WriteLine("Error: " + e.Message); } if (System.Diagnostics.Debugger.IsAttached) { Console.Write("\nPress enter...\n"); Console.Read(); } }
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> /// 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; }
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! }
static void Main( string[] args ) { try { int curArgIndex = 0; bool bFragment = false; // decode options XmlDiffOptions options = XmlDiffOptions.None; string optionsString = string.Empty; while ( curArgIndex < args.Length && ( args[curArgIndex][0] == '/' || args[curArgIndex][0] == '-' ) ) { if ( args[curArgIndex].Length != 2 ) { System.Console.Write( "Invalid option: " + args[curArgIndex] + "\n" ); return; } switch ( args[curArgIndex][1] ) { case '?': WriteUsage(); return; case 'o': options |= XmlDiffOptions.IgnoreChildOrder; break; case 'c': options |= XmlDiffOptions.IgnoreComments; break; case 'p': options |= XmlDiffOptions.IgnorePI; break; case 'w': options |= XmlDiffOptions.IgnoreWhitespace; break; case 'n': options |= XmlDiffOptions.IgnoreNamespaces; break; case 'r': options |= XmlDiffOptions.IgnorePrefixes; break; case 'x': options |= XmlDiffOptions.IgnoreXmlDecl; break; case 'd': options |= XmlDiffOptions.IgnoreDtd; break; case 'f': bFragment = true; break; default: System.Console.Write( "Invalid option: " + args[curArgIndex] + "\n" ); return; } optionsString += args[curArgIndex][1]; curArgIndex++; } if ( args.Length - curArgIndex < 3 ) { WriteUsage(); return; } bool includeIngnore = (args.Length - curArgIndex > 3); string sourceXmlFile = args[curArgIndex]; string changedXmlFile = args[curArgIndex+1]; string resultHtmlViewFile = args[curArgIndex+2]; string diffgramFile = args[curArgIndex + 3]; string ignoreFile = includeIngnore ? args[curArgIndex + 4] : null; MemoryStream diffgram = new MemoryStream(); XmlTextWriter diffgramWriter = new XmlTextWriter( new StreamWriter( diffgram ) ); var msg = "Comparing " + sourceXmlFile + " to " + changedXmlFile + " using ignore config " + ignoreFile; log.Info(msg); Console.WriteLine(msg); XmlDiff xmlDiff = new XmlDiff( options ); bool bIdentical = true; try { bIdentical = xmlDiff.Compare(sourceXmlFile, changedXmlFile, bFragment, diffgramWriter, ignoreFile); } catch (Exception e) { log.Error(e.Message, e); Console.WriteLine(e.Message); return; } msg = "Files compared " + (bIdentical ? "identical." : "different."); log.Info(msg); Console.WriteLine(msg); msg = "Generating " + resultHtmlViewFile + " view file"; log.Info(msg); Console.WriteLine(msg); TextWriter resultHtml = new StreamWriter( new FileStream( resultHtmlViewFile, FileMode.Create, FileAccess.Write )); 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>" + sourceXmlFile + "</th><th>" + changedXmlFile + "</th></tr>" + "<tr><td colspan=2><hr size=1></td></tr>" ); if ( bIdentical ) { 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 ); Microsoft.XmlDiffPatch.XmlDiffView xmlDiffView = new Microsoft.XmlDiffPatch.XmlDiffView(); XmlTextReader sourceReader; if ( bFragment ) { NameTable nt = new NameTable(); sourceReader = new XmlTextReader( new FileStream( sourceXmlFile, FileMode.Open, FileAccess.Read ), XmlNodeType.Element, new XmlParserContext( nt, new XmlNamespaceManager( nt ), string.Empty, XmlSpace.Default ) ); } else { sourceReader = new XmlTextReader( sourceXmlFile ); } sourceReader.XmlResolver = null; xmlDiffView.Load( sourceReader, new XmlTextReader( diffgram ) ); xmlDiffView.GetHtml( resultHtml ); resultHtml.WriteLine( "</table></table></body></html>" ); resultHtml.Close(); msg = resultHtmlViewFile + " saved successfully."; log.Info(msg); Console.WriteLine(msg); msg = "saving diffgram file"; log.Info(msg); Console.WriteLine(msg); using (FileStream file = new FileStream(diffgramFile, FileMode.Create, FileAccess.Write)) { diffgram.WriteTo(file); } msg = "diffgram file saved to " + diffgramFile; log.Info(msg); Console.WriteLine(msg); } catch ( Exception e ) { log.Error(e.Message, e); Console.WriteLine( "Error: " + e.Message ); } if ( System.Diagnostics.Debugger.IsAttached ) { Console.Write( "\nPress enter...\n" ); Console.Read(); } }