Пример #1
0
        /// <summary>
        /// Writes data contained in XML document object to path stored in DocumentFilePath property.
        /// </summary>
        /// <param name="xmlDoc">XML formatted document object.</param>
        /// <returns>True if output operation is successful. False if write fails.</returns>
        public bool WriteDataToDocument(XmlDocument xmlDoc)
        {
            bool      success = true;
            DataTable dt      = _importer.ImportXmlDocumentToDataTable(xmlDoc);

            success = WriteDataToDocument(dt);
            return(success);
        }
Пример #2
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());
            }
        }