示例#1
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();
                }
            }
        }
示例#2
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();
                }
            }
        }
示例#3
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());
            }
        }