//------------------------------------------------------------------------- /* Transform and convert xml to table, get data to Parent.DT, field names to Fields */ void LoadXmlData(bool fieldsOnly = false) { Fields = new List <string>(); DataTable dt; try { XmlDocument xml = XmlController.GetXml(CommonProc.GetFilePath(NameXml), Codepage); if (!XsltNone) { xml = XmlController.Transform(xml, XsltFromFile ? CommonProc.GetFilePath(NameXslt) : XsltScript, XsltFromFile); } if (UseFieldsMap) { dt = XmlController.GetData(xml, PathRow, FieldsMap, fieldsOnly); // getting data based on FieldMap } else { dt = XmlController.GetData(xml, PathRow, DataFromTag, DataFromAttr, fieldsOnly); // getting data from tags/attributes } Fields = dt.Columns.OfType <DataColumn>().Select(x => x.ColumnName).ToList(); if (!fieldsOnly) { Parent.DT = dt.Copy(); Parent.DT.TableName = Parent.Name; } } catch (Exception ex) { throw new Exception(Parent.Name + ": " + ex.Message, ex); } }
//------------------------------------------------------------------------- /* Source command processing */ object CommandSource(Source src, string command, object param) { object ret = null; try { switch (command) { case "SelectFileCsv": src.CsvSource.LoadCsv(view.LoadFile(dataFolder, @"CSV|*.csv;*.txt|ALL|*.*", "csv")); if (File.Exists(CommonProc.GetFilePath(src.CsvSource.Filename))) { dataFolder = Path.GetDirectoryName(CommonProc.GetFilePath(src.CsvSource.Filename)); } break; case "ViewDataCsv": src.CsvSource.GetData(null, null); ret = Tuple.Create <object, string, string>(src.DT, src.CsvSource.Filename, string.Format("{0} rows", src.DT == null ? 0 : src.DT.Rows.Count)); break; case "SelectFileExcel": string fn = view.LoadFile(dataFolder, @"Excel|*.xl*|ALL|*.*", "xls"); ret = (!string.IsNullOrEmpty(fn) && File.Exists(fn)); if ((bool)ret) { src.ExcelSource.Filename = fn; dataFolder = Path.GetDirectoryName(fn); } break; case "SetExcelSeets": src.ExcelSource.Sheets = (param as Tuple <List <string>, string>).Item1; src.ExcelSource.Sheet = (param as Tuple <List <string>, string>).Item2; break; case "ViewDataExcel": src.ExcelSource.GetData(null, null); ret = Tuple.Create <object, string, string>(src.DT, src.ExcelSource.Filename, string.Format("{0} rows", src.DT == null ? 0 : src.DT.Rows.Count)); break; case "RunXmlOptionsPrepare": ret = Tuple.Create <object, string>(src.XmlSource, "Xml Options for " + src.Name); break; case "GetXmlFields": src.XmlSource.GetCheckFields(); break; default: break; } } catch (Exception ex) { OnError(string.Format("Error process command \"{0}\" with source \"{1}\"", command, src.Name), ex); } return(ret); }
//~~~~~~~~ Convert ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #region //------------------------------------------------------------------------- private void bConvert_Click(object sender, EventArgs e) { Cursor = Cursors.WaitCursor; try { bs.EndEdit(); bsFields.EndEdit(); tbResult.Clear(); tbResult.ClearUndo(); dgResult.DataSource = null; XmlController.Dt = null; GC.Collect(); statusLabel.Text = ""; dgResult.ContextMenuStrip = null; Refresh(); XmlDocument xml = XmlController.GetXml(CommonProc.GetFilePath(tbNameXml.Text), tbCodepage.Text); if (!rbXsltNotUse.Checked) { xml = XmlController.Transform(xml, rbXsltFile.Checked ? CommonProc.GetFilePath(tbNameXslt.Text) : tbXslt.Text, rbXsltFile.Checked); } if (xml != null) { tbResult.Language = xml.OuterXml.Length > MAX_XMLBYTES_FOR_COLOR ? Language.Custom : Language.HTML; tbResult.Text = xml.OuterXml; } if (cbUseFieldsMap.Checked) { XmlController.Dt = XmlController.GetData(xml, tbPathRow.Text, (List <FieldMap>)bsFields.DataSource); } else { XmlController.Dt = XmlController.GetData(xml, tbPathRow.Text, cbFromTag.Checked, cbFromAttrib.Checked); } dgResult.DataSource = XmlController.Dt; statusLabel.Text = string.Format("{0} rows converted", XmlController.Dt.Rows.Count); if (XmlController.Dt.Rows.Count > 0) { dgResult.ContextMenuStrip = cmsResult; } tabs.SelectedTab = tabPageResult; } catch (Exception ex) { #if DEBUG MessageBox.Show(string.Format("{0}\n{1}", ex.Message, ex.InnerException), "Convert error", MessageBoxButtons.OK, MessageBoxIcon.Error); #else MessageBox.Show(ex.Message, "Convert error", MessageBoxButtons.OK, MessageBoxIcon.Error); #endif } finally { Cursor = Cursors.Default; } }
//------------------------------------------------------------------------- public override void Check() { if (string.IsNullOrEmpty(NameXml) || !File.Exists(CommonProc.GetFilePath(NameXml))) { throw new Exception(Parent.Name + ": XML file not exist"); } if (XsltType == XlstProcType.File && (string.IsNullOrEmpty(NameXslt) || !File.Exists(CommonProc.GetFilePath(NameXslt)))) { throw new Exception(Parent.Name + ": XSLT file not exist"); } }
//------------------------------------------------------------------------- public override void Check() { if (string.IsNullOrEmpty(Filename)) { throw new Exception(Parent.Name + ": file name not defined"); } if (!File.Exists(CommonProc.GetFilePath(Filename))) { throw new Exception(Parent.Name + ": file not exist"); } }
//------------------------------------------------------------------------- /* Get data from file to Parent.DT, field names to Fields */ void LoadCsvData(bool fieldsOnly = false) { Fields = new List <string>(); if (string.IsNullOrEmpty(Filename) || !File.Exists(CommonProc.GetFilePath(Filename))) { return; } Encoding enc = Encoding.Default; if (!string.IsNullOrEmpty(Codepage)) { string cp = Codepage.ToUpper().Trim(); int cpNo = Encoding.Default.CodePage; switch (cp) { case "UTF8": enc = Encoding.UTF8; break; case "UTF7": enc = Encoding.UTF7; break; case "UTF16": enc = Encoding.Unicode; break; case "UTF32": enc = Encoding.UTF32; break; default: if (string.IsNullOrEmpty(cp)) { enc = Encoding.Default; } else if (int.TryParse(cp, out cpNo)) { enc = Encoding.GetEncoding(cpNo); } else { enc = Encoding.GetEncoding(cp); } break; } } using (StreamReader rdr = new StreamReader(CommonProc.GetFilePath(Filename), enc, true)) { string[] dlmts = new string[] { string.IsNullOrEmpty(Delimiter) ? Convert.ToString((char)9) : Delimiter }; string flds = rdr.ReadLine(); if (!string.IsNullOrEmpty(flds)) { Fields = flds.Split(dlmts, StringSplitOptions.None).ToList(); for (int i = 0; i < Fields.Count; i++) { if (!FirstLineNames || string.IsNullOrEmpty(Fields[i])) { Fields[i] = string.Format("Field{0}", i + 1); } } } if (!fieldsOnly) { Parent.DT = new DataTable(); for (int i = 0; i < Fields.Count; i++) { string tmpName = Fields[i]; int suff = 0; while (Parent.DT.Columns.OfType <DataColumn>().Count(x => x.ColumnName == Fields[i]) > 0) { Fields[i] = tmpName + "_" + (++suff).ToString(); } Parent.DT.Columns.Add(Fields[i]); } if (!FirstLineNames) { rdr.BaseStream.Seek(0, 0); rdr.DiscardBufferedData(); } while (rdr.Peek() >= 0) { DataRow dr = Parent.DT.NewRow(); string row = rdr.ReadLine(); if (string.IsNullOrEmpty(row)) { continue; } List <string> rowFields = row.Split(dlmts, StringSplitOptions.None).ToList(); for (int i = 0; i < Math.Min(Fields.Count, rowFields.Count); i++) { dr[i] = rowFields[i]; } Parent.DT.Rows.Add(dr); } Parent.DT.TableName = Parent.Name; } rdr.Close(); } }