コード例 #1
0
        private void RestoreOriginalAppSettings()
        {
            bool           importSucceeded = false;
            string         fileName        = string.Empty;
            PFDataImporter importer        = new PFDataImporter();
            DataTable      saveCurrentDt   = null;
            DataTable      importedDt      = null;
            DataTable      currentDt       = null;
            string         appName         = AppInfo.AssemblyProduct;

            try
            {
                saveCurrentDt           = this.keyValsDataSet.Tables["KeyValTable"].Copy();
                saveCurrentDt.TableName = "KeyValTable";

                fileName = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase), "pfSettingsOrig.xml").Replace(@"file:\", "");
                if (File.Exists(fileName) == false)
                {
                    _msg.Length = 0;
                    _msg.Append("Unable to find XML file containing original settings: ");
                    _msg.Append(fileName);
                    throw new System.Exception(_msg.ToString());
                }

                DialogResult res = ImportPrompt(fileName);
                if (res != DialogResult.Yes)
                {
                    return;
                }

                importedDt = importer.ImportXmlFileToDataTable(fileName);
                if (importedDt.TableName != "KeyValTable")
                {
                    _msg.Length = 0;
                    _msg.Append("Invalid table name in imported XML file: ");
                    _msg.Append(importedDt.TableName);
                    throw new System.Exception(_msg.ToString());
                }

                currentDt = this.keyValsDataSet.Tables["KeyValTable"];
                currentDt.Clear();

                for (int i = 0; i < importedDt.Rows.Count; i++)
                {
                    DataRow inrow  = importedDt.Rows[i];
                    DataRow outrow = currentDt.NewRow();
                    outrow.ItemArray = inrow.ItemArray;
                    currentDt.Rows.Add(outrow);
                }


                currentDt.AcceptChanges();

                UpdateConfigItems(true);

                importSucceeded = true;
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessageWithStackTrace(ex));
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToErrorLog);
            }
            finally
            {
                if (importSucceeded == false && saveCurrentDt.Rows.Count > 0)
                {
                    currentDt = this.keyValsDataSet.Tables["KeyValTable"];
                    currentDt.Clear();
                    for (int i = 0; i < saveCurrentDt.Rows.Count; i++)
                    {
                        DataRow inrow  = saveCurrentDt.Rows[i];
                        DataRow outrow = currentDt.NewRow();
                        outrow.ItemArray = inrow.ItemArray;
                        currentDt.Rows.Add(outrow);
                    }

                    currentDt.AcceptChanges();
                }
            }
        }
コード例 #2
0
        public static void ImportTextData(MainForm frm)
        {
            PFDataImporter dataImporter                                = new PFDataImporter();
            string         configValue                                 = string.Empty;
            string         dirName                                     = string.Empty;
            string         fileName                                    = string.Empty;
            string         textDelimDataFileName                       = "TableTestExtractDelim.txt";
            string         textDelimDataLineFormatFileName             = "TableTestExtractDelimLineFormat.xml";
            string         textFixedLengthDataFileName                 = "TableTestExtractFxl.txt";
            string         textFixedLengthDataLineFormatFileName       = "TableTestExtractFxlLineFormat.xml";
            string         textFixedLengthNoCrLfDataFileName           = "TableTestExtractFxlNoCrLf.txt";
            string         textFixedLengthNoCrLfDataLineFormatFileName = "TableTestExtractFxlNoCrLfLineFormat.xml";
            DataSet        dsDelimited                                 = null;
            DataSet        dsFixedLength                               = null;
            DataSet        dsFixedLengthNoCrLf                         = null;

            try
            {
                _msg.Length = 0;
                _msg.Append("ImportTextData started ...\r\n");
                Program._messageLog.WriteLine(_msg.ToString());

                //Get filenames
                configValue = AppConfig.GetStringValueFromConfigFile("textDelimDataFileName", "TableTestExtractDelim.txt");
                dirName     = Path.GetDirectoryName(configValue);
                fileName    = Path.GetFileName(configValue);
                if (String.IsNullOrEmpty(dirName))
                {
                    dirName = AppDomain.CurrentDomain.BaseDirectory;
                }
                if (String.IsNullOrEmpty(fileName))
                {
                    fileName = "TableTestExtractDelim.txt";
                }
                textDelimDataFileName = Path.Combine(dirName, fileName);

                configValue = AppConfig.GetStringValueFromConfigFile("textDelimDataLineFormatFileName", "TableTestExtractDelimLineFormat.xml");
                dirName     = Path.GetDirectoryName(configValue);
                fileName    = Path.GetFileName(configValue);
                if (String.IsNullOrEmpty(dirName))
                {
                    dirName = AppDomain.CurrentDomain.BaseDirectory;
                }
                if (String.IsNullOrEmpty(fileName))
                {
                    fileName = "TableTestExtractDelimLineFormat.xml";
                }
                textDelimDataLineFormatFileName = Path.Combine(dirName, fileName);

                configValue = AppConfig.GetStringValueFromConfigFile("textFixedLengthDataFileName", "TableTestExtractFxl.txt");
                dirName     = Path.GetDirectoryName(configValue);
                fileName    = Path.GetFileName(configValue);
                if (String.IsNullOrEmpty(dirName))
                {
                    dirName = AppDomain.CurrentDomain.BaseDirectory;
                }
                if (String.IsNullOrEmpty(fileName))
                {
                    fileName = "TableTestExtractFxl.txt";
                }
                textFixedLengthDataFileName = Path.Combine(dirName, fileName);

                configValue = AppConfig.GetStringValueFromConfigFile("textFixedLengthDataLineFormatFileName", "TableTestExtractFxlLineFormat.xml");
                dirName     = Path.GetDirectoryName(configValue);
                fileName    = Path.GetFileName(configValue);
                if (String.IsNullOrEmpty(dirName))
                {
                    dirName = AppDomain.CurrentDomain.BaseDirectory;
                }
                if (String.IsNullOrEmpty(fileName))
                {
                    fileName = "TableTestExtractFxlLineFormat.xmldar";
                }
                textFixedLengthDataLineFormatFileName = Path.Combine(dirName, fileName);

                configValue = AppConfig.GetStringValueFromConfigFile("textFixedLengthNoCrLfDataFileName", "TableTestExtractFxlNoCrLf.txt");
                dirName     = Path.GetDirectoryName(configValue);
                fileName    = Path.GetFileName(configValue);
                if (String.IsNullOrEmpty(dirName))
                {
                    dirName = AppDomain.CurrentDomain.BaseDirectory;
                }
                if (String.IsNullOrEmpty(fileName))
                {
                    fileName = "TableTestExtractFxlNoCrLf.txt";
                }
                textFixedLengthNoCrLfDataFileName = Path.Combine(dirName, fileName);

                configValue = AppConfig.GetStringValueFromConfigFile("textFixedLengthNoCrLfDataLineFormatFileName", "TableTestExtractFxlNoCrLfLineFormat.xml");
                dirName     = Path.GetDirectoryName(configValue);
                fileName    = Path.GetFileName(configValue);
                if (String.IsNullOrEmpty(dirName))
                {
                    dirName = AppDomain.CurrentDomain.BaseDirectory;
                }
                if (String.IsNullOrEmpty(fileName))
                {
                    fileName = "TableTestExtractFxlNoCrLfLineFormat.xmldar";
                }
                textFixedLengthNoCrLfDataLineFormatFileName = Path.Combine(dirName, fileName);

                _msg.Length = 0;
                _msg.Append(Environment.NewLine);
                _msg.Append("Output text to DataSet and DataTable test: ");
                _msg.Append(Environment.NewLine);
                _msg.Append(textDelimDataFileName);
                _msg.Append(Environment.NewLine);
                _msg.Append(textDelimDataLineFormatFileName);
                _msg.Append(Environment.NewLine);
                _msg.Append(textFixedLengthDataFileName);
                _msg.Append(Environment.NewLine);
                _msg.Append(textFixedLengthDataLineFormatFileName);
                _msg.Append(Environment.NewLine);
                _msg.Append(textFixedLengthNoCrLfDataFileName);
                _msg.Append(Environment.NewLine);
                _msg.Append(textFixedLengthNoCrLfDataLineFormatFileName);
                _msg.Append(Environment.NewLine);
                Program._messageLog.WriteLine(_msg.ToString());

                //run PFDataImporter routines

                dsDelimited         = dataImporter.ImportDelimitedTextFileToDataSet(textDelimDataFileName, textDelimDataLineFormatFileName);
                dsFixedLength       = dataImporter.ImportFixedLengthTextFileToDataSet(textFixedLengthDataFileName, textFixedLengthDataLineFormatFileName);
                dsFixedLengthNoCrLf = dataImporter.ImportFixedLengthTextFileToDataSet(textFixedLengthNoCrLfDataFileName, textFixedLengthNoCrLfDataLineFormatFileName);


                _msg.Length = 0;
                _msg.Append(Environment.NewLine);
                _msg.Append("dsDelimited rows: ");
                _msg.Append(dsDelimited.Tables[0].Rows.Count.ToString());
                _msg.Append(Environment.NewLine);
                _msg.Append("dsFixedLength rows: ");
                _msg.Append(dsFixedLength.Tables[0].Rows.Count.ToString());
                _msg.Append(Environment.NewLine);
                _msg.Append("dsFixedLengthNoCrLf rows: ");
                _msg.Append(dsFixedLengthNoCrLf.Tables[0].Rows.Count.ToString());
                _msg.Append(Environment.NewLine);
                Program._messageLog.WriteLine(_msg.ToString());
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                Program._messageLog.WriteLine(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                _msg.Length = 0;
                _msg.Append("\r\n... ImportTextData finished.");
                Program._messageLog.WriteLine(_msg.ToString());
            }
        }
コード例 #3
0
        private void ImportFromXmlFile()
        {
            bool           importSucceeded = false;
            string         fileName        = string.Empty;
            PFDataImporter importer        = new PFDataImporter();
            DataTable      saveCurrentDt   = null;
            DataTable      importedDt      = null;
            DataTable      currentDt       = null;

            if (ShowOpenFileDialog() != DialogResult.OK)
            {
                return;
            }

            try
            {
                saveCurrentDt           = this.keyValsDataSet.Tables["KeyValTable"].Copy();
                saveCurrentDt.TableName = "KeyValTable";

                fileName = _openFileDialog.FileName;

                DialogResult res = ImportPrompt(fileName);
                if (res != DialogResult.Yes)
                {
                    return;
                }

                importedDt = importer.ImportXmlFileToDataTable(fileName);
                if (importedDt.TableName != "KeyValTable")
                {
                    _msg.Length = 0;
                    _msg.Append("Invalid table name in imported XML file: ");
                    _msg.Append(importedDt.TableName);
                    throw new System.Exception(_msg.ToString());
                }

                currentDt = this.keyValsDataSet.Tables["KeyValTable"];
                currentDt.Clear();
                //currentDt = importedDt.Copy();
                //currentDt.AcceptChanges();

                for (int i = 0; i < importedDt.Rows.Count; i++)
                {
                    DataRow inrow  = importedDt.Rows[i];
                    DataRow outrow = currentDt.NewRow();
                    outrow.ItemArray = inrow.ItemArray;
                    currentDt.Rows.Add(outrow);
                }


                currentDt.AcceptChanges();


                importSucceeded = true;
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToErrorLog);
            }
            finally
            {
                if (importSucceeded == false && saveCurrentDt.Rows.Count > 0)
                {
                    currentDt = this.keyValsDataSet.Tables["KeyValTable"];
                    currentDt.Clear();
                    for (int i = 0; i < saveCurrentDt.Rows.Count; i++)
                    {
                        DataRow inrow  = saveCurrentDt.Rows[i];
                        DataRow outrow = currentDt.NewRow();
                        outrow.ItemArray = inrow.ItemArray;
                        currentDt.Rows.Add(outrow);
                    }


                    currentDt.AcceptChanges();
                }
            }
        }
コード例 #4
0
        public static void ImportXmlDocument(MainForm frm)
        {
            string         configValue              = string.Empty;
            string         dirName                  = string.Empty;
            string         fileName                 = string.Empty;
            string         xmlNoSchemaFileName      = string.Empty;
            string         xmlWithSchemaFileName    = string.Empty;
            string         tabXmlNoSchemaFileName   = string.Empty;
            string         tabXmlWithSchemaFileName = string.Empty;
            PFDataImporter dataImporter             = new PFDataImporter();
            string         xmlString                = string.Empty;
            DataSet        dsNoSchema               = null;
            DataSet        dsWithSchema             = null;
            DataTable      dtNoSchema               = null;
            DataTable      dtWithSchema             = null;
            DataTable      dtWithSchema2            = null;

            try
            {
                _msg.Length = 0;
                _msg.Append("ImportXmlDocument started ...\r\n");
                Program._messageLog.WriteLine(_msg.ToString());

                configValue = AppConfig.GetStringValueFromConfigFile("XmlNoSchemaFileName", "TestXmlNoSchema.xml");
                dirName     = Path.GetDirectoryName(configValue);
                fileName    = Path.GetFileName(configValue);
                if (String.IsNullOrEmpty(dirName))
                {
                    dirName = AppDomain.CurrentDomain.BaseDirectory;
                }
                if (String.IsNullOrEmpty(fileName))
                {
                    fileName = "TestXmlNoSchema.xml";
                }
                xmlNoSchemaFileName = Path.Combine(dirName, fileName);

                configValue = AppConfig.GetStringValueFromConfigFile("XmlWithSchemaFileName", "TestXmlWithSchema.xml");
                dirName     = Path.GetDirectoryName(configValue);
                fileName    = Path.GetFileName(configValue);
                if (String.IsNullOrEmpty(dirName))
                {
                    dirName = AppDomain.CurrentDomain.BaseDirectory;
                }
                if (String.IsNullOrEmpty(fileName))
                {
                    fileName = "TestXmlWithSchema.xml";
                }
                xmlWithSchemaFileName = Path.Combine(dirName, fileName);

                configValue = AppConfig.GetStringValueFromConfigFile("TabXmlNoSchemaFileName", "TestTabXmlNoSchema.xml");
                dirName     = Path.GetDirectoryName(configValue);
                fileName    = Path.GetFileName(configValue);
                if (String.IsNullOrEmpty(dirName))
                {
                    dirName = AppDomain.CurrentDomain.BaseDirectory;
                }
                if (String.IsNullOrEmpty(fileName))
                {
                    fileName = "TesTabtXmlNoSchema.xml";
                }
                tabXmlNoSchemaFileName = Path.Combine(dirName, fileName);

                configValue = AppConfig.GetStringValueFromConfigFile("TabXmlWithSchemaFileName", "TestTabXmlWithSchema.xml");
                dirName     = Path.GetDirectoryName(configValue);
                fileName    = Path.GetFileName(configValue);
                if (String.IsNullOrEmpty(dirName))
                {
                    dirName = AppDomain.CurrentDomain.BaseDirectory;
                }
                if (String.IsNullOrEmpty(fileName))
                {
                    fileName = "TestTabXmlWithSchema.xml";
                }
                tabXmlWithSchemaFileName = Path.Combine(dirName, fileName);


                _msg.Length = 0;
                _msg.Append(Environment.NewLine);
                _msg.Append("Output to DataSet and DataTable test: ");
                _msg.Append(Environment.NewLine);
                _msg.Append(xmlNoSchemaFileName);
                _msg.Append(Environment.NewLine);
                _msg.Append(xmlWithSchemaFileName);
                _msg.Append(Environment.NewLine);
                Program._messageLog.WriteLine(_msg.ToString());

                XmlDocument xmlDocNoSchema = new XmlDocument();
                xmlDocNoSchema.Load(xmlNoSchemaFileName);
                XmlDocument xmlDocWithSchema = new XmlDocument();
                xmlDocWithSchema.Load(xmlWithSchemaFileName);
                XmlDocument tabXmlDocNoSchema = new XmlDocument();
                tabXmlDocNoSchema.Load(tabXmlNoSchemaFileName);
                XmlDocument tabXmlDocWithSchema = new XmlDocument();
                tabXmlDocWithSchema.Load(tabXmlWithSchemaFileName);

                dsNoSchema   = dataImporter.ImportXmlDocumentToDataSet(xmlDocNoSchema);
                dsWithSchema = dataImporter.ImportXmlDocumentToDataSet(xmlDocWithSchema);
                try
                {
                    dtNoSchema = dataImporter.ImportXmlDocumentToDataTable(tabXmlDocNoSchema);
                }
                catch (System.Exception ex)
                {
                    _msg.Length = 0;
                    _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                    Program._messageLog.WriteLine(_msg.ToString());
                }
                dtWithSchema = dataImporter.ImportXmlDocumentToDataTable(tabXmlDocWithSchema);

                _msg.Length = 0;
                _msg.Append(Environment.NewLine + Environment.NewLine);
                _msg.Append("DS NoSchema rows: ");
                _msg.Append(dsNoSchema.Tables[0].Rows.Count.ToString());
                _msg.Append(Environment.NewLine);
                _msg.Append("DS WithSchema rows: ");
                _msg.Append(dsWithSchema.Tables[0].Rows.Count.ToString());
                _msg.Append(Environment.NewLine + Environment.NewLine);
                _msg.Append("DT NoSchema rows: ");
                if (dtNoSchema != null)
                {
                    _msg.Append(dtNoSchema.Rows.Count.ToString());
                }
                else
                {
                    _msg.Append("Schema missing. Is required for DataTable object.");
                }
                _msg.Append(Environment.NewLine);
                _msg.Append("DT WithSchema rows: ");
                _msg.Append(dtWithSchema.Rows.Count.ToString());
                _msg.Append(Environment.NewLine + Environment.NewLine);
                Program._messageLog.WriteLine(_msg.ToString());

                dsNoSchema.Tables[0].WriteXml(@"c:\temp\dtNoSchema.xml");
                dsWithSchema.Tables[0].WriteXml(@"c:\temp\dtWithSchema.xml", XmlWriteMode.WriteSchema);
                dsWithSchema.Tables[0].WriteXmlSchema(@"c:\temp\dtSchema.xsd");

                //dtNoSchema = dataImporter.ImportXmlDocumentToDataTable(xmlDocNoSchema);
                //dtWithSchema = dataImporter.ImportXmlDocumentToDataTable(xmlDocWithSchema);
                //dtWithSchema = dataImporter.ImportXmlFileToDataTable(xmlWithSchemaFileName);
                //dtWithSchema = dataImporter.ImportXmlFileToDataTable(@"c:\temp\dtWithSchema.xml");
                dtWithSchema  = dataImporter.ImportXmlFileToDataTable(@"c:\temp\dtWithSchemaMod10Rows.xml");
                dtWithSchema2 = dataImporter.ImportXmlFileToDataTable(@"c:\temp\testdata.xml");

                _msg.Length = 0;
                _msg.Append(Environment.NewLine + Environment.NewLine);
                //_msg.Append("DT NoSchema rows: ");
                //_msg.Append(dtNoSchema.Rows.Count.ToString());
                //_msg.Append(Environment.NewLine);
                _msg.Append("DT WithSchema rows: ");
                _msg.Append(dtWithSchema.Rows.Count.ToString());
                _msg.Append(Environment.NewLine + Environment.NewLine);
                _msg.Append("DT WithSchema 2 rows: ");
                _msg.Append(dtWithSchema2.Rows.Count.ToString());
                _msg.Append(Environment.NewLine + Environment.NewLine);
                Program._messageLog.WriteLine(_msg.ToString());

                _msg.Length = 0;

                dtNoSchema    = new DataTable();
                dtNoSchema    = dataImporter.ImportXmlFileToDataTable(@"c:\temp\dtNoSchema.xml");
                dtWithSchema  = new DataTable();
                dtWithSchema  = dataImporter.ImportXmlFileToDataTable(@"c:\temp\dtWithSchema.xml");
                dtWithSchema2 = new DataTable();
                dtWithSchema2 = dataImporter.ImportXmlFileToDataTable(@"c:\temp\dtNoSchema.xml", @"c:\temp\dtSchema.xsd");


                _msg.Length = 0;
                _msg.Append(Environment.NewLine + Environment.NewLine);
                _msg.Append("Testing new dataImporter xml and xml schema importing ...");
                _msg.Append(Environment.NewLine + Environment.NewLine);
                _msg.Append("DT NoSchema imported rows:     ");
                _msg.Append(dtNoSchema.Rows.Count.ToString());
                _msg.Append(Environment.NewLine);
                _msg.Append("DT NoSchema state maxlen:      ");
                _msg.Append(dtNoSchema.Columns[2].MaxLength.ToString());
                _msg.Append(Environment.NewLine);
                _msg.Append("DT WithSchema imported rows:   ");
                _msg.Append(dtWithSchema.Rows.Count.ToString());
                _msg.Append(Environment.NewLine);
                _msg.Append("DT WithSchema state maxlen:    ");
                _msg.Append(dtWithSchema.Columns[2].MaxLength.ToString());
                _msg.Append(Environment.NewLine);
                _msg.Append("DT WithSchema2 imported rows:  ");
                _msg.Append(dtWithSchema2.Rows.Count.ToString());
                _msg.Append(Environment.NewLine);
                _msg.Append("DT WithSchema2 state maxlen:   ");
                _msg.Append(dtWithSchema2.Columns[2].MaxLength.ToString());
                _msg.Append(Environment.NewLine);
                Program._messageLog.WriteLine(_msg.ToString());


                //DemonstrateReadWriteXMLDocumentWithString();
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                Program._messageLog.WriteLine(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                _msg.Length = 0;
                _msg.Append("\r\n... ImportXmlDocument finished.");
                Program._messageLog.WriteLine(_msg.ToString());
            }
        }