public Form2(Form1 form1, List<Clerk> ClerksList, GaryLib.ConfigFileInfo fileInfo) { InitializeComponent(); // TODO: Complete member initialization this.form1 = form1; this.ClerksList = ClerksList; this.fileInfo = fileInfo; dataGridView1.Rows.Clear(); dataGridView1.RowCount = ClerksList.Count; for (int i = 0; i < ClerksList.Count; i++) { dataGridView1.Rows[i].HeaderCell.Value = (i + 1).ToString(); dataGridView1[0, i].Value = ClerksList[i].ID; dataGridView1[1, i].Value = ClerksList[i].Name; dataGridView1[2, i].Value = ClerksList[i].Position; dataGridView1[3, i].Value = ClerksList[i].Mobile; dataGridView1[4, i].Value = ClerksList[i].Office; dataGridView1[5, i].Value = ClerksList[i].Dept; } dataGridView2.Rows.Clear(); dataGridView2.RowCount = ClerksList.Count; for (int i = 0; i < ClerksList.Count; i++) { dataGridView2.Rows[i].HeaderCell.Value = (i + 1).ToString(); dataGridView2[0, i].Value = ClerksList[i].ID; dataGridView2[1, i].Value = ClerksList[i].Name; dataGridView2[2, i].Value = ClerksList[i].Position; dataGridView2[3, i].Value = ClerksList[i].Mobile; dataGridView2[4, i].Value = ClerksList[i].Office; dataGridView2[5, i].Value = ClerksList[i].Dept; SelectingClerksList.Add(ClerksList[i]); } if (dataGridView2.SelectedRows.Count > 0) { dataGridView2.SelectedRows[0].Selected = false; } }
/// <summary> /// 根据指定文件路径创建XML文件 /// </summary> /// <param name="FilePathPlusName">指定文件路径</param> /// <returns>返回是否顺利完成写XML文件工作</returns> public static bool WriteXML(ConfigFileInfo fileInfo) { ///使用方法: ///if (!ReadWriteXML.WriteXML(BFGs)) ///{ /// MessageBox.Show(ReadWriteXML.ErrorString); ///} if (WritingXML(fileInfo)) { //如果成功写文件,将原数据文件覆盖 File.Replace(@"PathDataTmp.xml", @"PathData.xml", @"PathDataBak.xml"); //删除覆盖时的源备份文件 File.Delete(@"PathDataBak.xml"); return true; } else { return false; } }
public static bool ReadXML(ConfigFileInfo fileInfo) { ///使用方法: ///if (!ReadWriteXML.WriteXML(BFGs)) ///{ /// MessageBox.Show(ReadWriteXML.ErrorString); ///} bool FileExsit = false; try { //实例化xml XmlDocument xmlDoc = new XmlDocument(); //设置xml读取时忽略注释部分 XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; XmlReader reader = XmlReader.Create(@"PathData.xml", settings); //读取xml文件 xmlDoc.Load(reader); FileExsit = true; //得到根节点-高炉组信息 XmlNode RootNode = xmlDoc.SelectSingleNode("信息"); // 得到高炉组列表 XmlNodeList INFOsxnl = RootNode.ChildNodes; // 将节点转换为元素,便于得到节点的属性值 XmlElement INFOxe = RootNode as XmlElement; //XML读取的节点索引,读取过程中逐步加1 int XmlReadStep = 0; ////XML读取的子节点索引,读取过程中逐步加1 //int IndexStep = 0;; fileInfo.ExcelFilePath = INFOxe.ChildNodes[XmlReadStep++].InnerText; fileInfo.DBFilePath = INFOxe.ChildNodes[XmlReadStep++].InnerText; reader.Close(); return true; } catch (Exception ex) { ErrorString = ex.ToString(); if (FileExsit) { MessageBox.Show("本地没有配置文件,将在本地创建默认配置文件"); } else { if (MessageBox.Show("配置文件已损坏,是否重新创建配置文件。选择“是”将创建默认配置文件并覆盖原始数据,如果不希望本地原始数据丢失,请选择“否”", "配置信息读出过程出错", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes) { XmlTextWriter writer = new XmlTextWriter(@"PathData.xml", null); writer.Close(); if (!ReadWriteXML.WriteXML(new ConfigFileInfo())) { MessageBox.Show(ReadWriteXML.ErrorString); } } } return false; } }
static bool WritingXML(ConfigFileInfo fileInfo) { try { XmlTextWriter writer = new XmlTextWriter(@"PathDataTmp.xml", null); //使用自动缩进便于阅读 writer.Formatting = Formatting.Indented; //写入根元素-信息 writer.WriteStartElement("信息"); writer.WriteElementString("Excel文件路径绝对地址", fileInfo.ExcelFilePath); writer.WriteElementString("DB文件路径绝对地址", fileInfo.DBFilePath); //关闭根元素“信息”,并书写结束标签 writer.WriteEndElement(); //将XML写入文件并且关闭XmlTextWriter writer.Close(); return true; } catch (Exception ex) { ErrorString = ex.ToString(); return false; } }