private void btn_Import_Click(object sender, EventArgs e) { var fpath = Utils.ChooseOpenFile("从 xml 文件中导入数据", StaticCriterions.FileExtensionFilter, false); if (fpath != null) { var sb = new StringBuilder(); var succ = false; var newData = XmlSerializer.ImportFromXml(fpath[0], typeof(StaticCriterions), out succ, ref sb) as StaticCriterions; if (succ) { Button activeButton = null; foreach (var cr in _criterionButtons) { cr.Key.Tag = newData.Criterions[cr.Value]; if (ActiveCriterion.GetType() == newData.Criterions[cr.Value].GetType()) { activeButton = cr.Key; } } if (activeButton != null) { CriterionBtnOnClick(activeButton, null); } } } }
private void ExportSectionInfoToText(SubgradeSection[] allSections) { if (allSections.Length > 0) { var infoPath = Utils.ChooseSaveFile("数据输出的文本", "文本(*.csv) | *.csv"); if (infoPath == null) { return; } // var methods = new List <dynamic>(); var properties = typeof(SectionInfo).GetProperties(BindingFlags.Public | BindingFlags.Instance); var fields = typeof(SectionInfo).GetFields(BindingFlags.Public | BindingFlags.Instance); methods.AddRange(properties); methods.AddRange(fields); methods.Sort(SortPropertiesByName); // var sb = new StringBuilder(); // 添加表头 foreach (var f in methods) { sb.Append(f.Name + ","); } sb.AppendLine(); // 添加数据行 foreach (var ss in allSections) { var xdata = ss.XData; foreach (var f in methods) { string strValue = null; var v = f.GetValue(xdata); if (v is double) { strValue = v.ToString("0.###"); } else if (v is Handle) { strValue = "'" + ((Handle)v).ToString(); } else if (v == null) { strValue = "NULL"; } else { strValue = v.ToString(); } sb.Append($"{strValue},"); } sb.AppendLine(); } using (var sw = new StreamWriter(infoPath)) { sw.WriteLine(sb.ToString()); } } }
private void btn_ExportData_Click(object sender, EventArgs e) { if (ActiveSpCriterion != null) { var fpath = Utils.ChooseSaveFile("导出数据到到 xml 文件", AutoProtectionCriterions.FileExtension); if (fpath != null) { var sb = new StringBuilder(); var succ = XmlSerializer.ExportToXmlFile(fpath, ActiveSpCriterion, ref sb); if (succ) { MessageBox.Show("数据导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } } }
public static AutoProtectionCriterions ImportAutoProtFromXml() { var fpath = Utils.ChooseOpenFile("从 xml 文件中导入数据", AutoProtectionCriterions.FileExtension, false); if (fpath != null) { var sb = new StringBuilder(); var succ = false; var newData = XmlSerializer.ImportFromXml(fpath[0], typeof(AutoProtectionCriterions), out succ, ref sb); if (succ) { return(newData as AutoProtectionCriterions); } } return(null); }
private void btn_Export_Click(object sender, EventArgs e) { var scs = StaticCriterions.UniqueInstance; if (scs != null) { var fpath = Utils.ChooseSaveFile("导出数据到到 xml 文件", StaticCriterions.FileExtensionFilter); if (fpath != null) { var errMsg = new StringBuilder(); var succ = XmlSerializer.ExportToXmlFile(fpath, scs, ref errMsg); if (succ) { System.Windows.Forms.MessageBox.Show("数据导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { System.Windows.Forms.MessageBox.Show("数据导出出错!" + "\r\n" + errMsg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }