Exemplo n.º 1
0
        /// <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;
        }