/// <summary> /// 打开文件夹 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpenFolder_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "XML文件|*.xml;*.XML"; if (dialog.ShowDialog() == DialogResult.OK) { bomFilePath = dialog.FileName; bomFileName = dialog.SafeFileName; bomDirPath = bomFilePath.Substring(0, bomFilePath.Length - bomFileName.Length - 1); txtBomFilePath.Text = dialog.SafeFileName; docModels.Clear(); lvFileInfo.Items.Clear(); //新建设计项目 if (designType.Equals(1)) { RootNewModel model = GetProductDataByLocalXML(dialog.FileName); for (int i = 0; i < docModels.Count; i++) { string docFilePathA = string.Format("{0}\\{1}\\{2}", bomDirPath, bomFileName.Substring(0, bomFileName.LastIndexOf('.')), docModels[i].Filename); string docFilePathB = string.Format("{0}\\{1}", bomDirPath, docModels[i].Filename); if (File.Exists(docFilePathA)) { docFilePaths.Add(docFilePathA); taskQueue.Enqueue(new KeyValuePair <int, string>(i, docFilePathA)); FileInfo info = new FileInfo(docFilePathA); ListViewItem lvi = new ListViewItem((i + 1).ToString()); lvi.SubItems.Add(docModels[i].Filename); lvi.SubItems.Add(Math.Ceiling(info.Length / 1024.0) >= 1024 ? ((Math.Ceiling(info.Length / 1024.0) / 1024).ToString("F2") + "MB") : (Math.Ceiling(info.Length / 1024.0).ToString("F2") + "KB")); lvi.SubItems.Add("0.00%"); lvi.SubItems.Add(FileUpLoadModel.FileUpLoadStateEnum.未开始.ToString()); lvi.BackColor = i % 2 == 0 ? Color.FromArgb(250, 250, 250) : Color.FromArgb(247, 247, 247); lvi.Tag = docModels[i]; lvFileInfo.Items.Add(lvi); } else if (File.Exists(docFilePathB)) { docFilePaths.Add(docFilePathB); taskQueue.Enqueue(new KeyValuePair <int, string>(i, docFilePathB)); FileInfo info = new FileInfo(docFilePathB); ListViewItem lvi = new ListViewItem((i + 1).ToString()); lvi.SubItems.Add(docModels[i].Filename); lvi.SubItems.Add(Math.Ceiling(info.Length / 1024.0) >= 1024 ? ((Math.Ceiling(info.Length / 1024.0) / 1024).ToString("F2") + "MB") : (Math.Ceiling(info.Length / 1024.0).ToString("F2") + "KB")); lvi.SubItems.Add("0.00%"); lvi.SubItems.Add(FileUpLoadModel.FileUpLoadStateEnum.未开始.ToString()); lvi.BackColor = i % 2 == 0 ? Color.FromArgb(250, 250, 250) : Color.FromArgb(247, 247, 247); lvi.Tag = docModels[i]; lvFileInfo.Items.Add(lvi); } else { MessageBox.Show("文件不完整!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtBomFilePath.Text = string.Empty; txtBomFilePath.Focus(); return; } } } //设计更改申请 else if (designType.Equals(2)) { RootChangeModel model = GetDocDataByLocalXML(dialog.FileName); for (int i = 0; i < model.Doc.Count; i++) { string docFilePathA = string.Format("{0}\\{1}\\{2}", bomDirPath, bomFileName.Substring(0, bomFileName.LastIndexOf('.')), model.Doc[i].Filename); string docFilePathB = string.Format("{0}\\{1}", bomDirPath, model.Doc[i].Filename); if (File.Exists(docFilePathA)) { docFilePaths.Add(docFilePathA); FileInfo info = new FileInfo(docFilePathA); ListViewItem lvi = new ListViewItem((i + 1).ToString()); lvi.SubItems.Add(model.Doc[i].Filename); lvi.SubItems.Add(Math.Ceiling(info.Length / 1024.0) >= 1024 ? ((Math.Ceiling(info.Length / 1024.0) / 1024).ToString("F2") + "MB") : (Math.Ceiling(info.Length / 1024.0).ToString("F2") + "KB")); lvi.SubItems.Add("0.00%"); lvi.SubItems.Add(FileUpLoadModel.FileUpLoadStateEnum.未开始.ToString()); lvi.BackColor = i % 2 == 0 ? Color.FromArgb(250, 250, 250) : Color.FromArgb(247, 247, 247); lvi.Tag = model.Doc[i]; lvFileInfo.Items.Add(lvi); } else if (File.Exists(docFilePathB)) { docFilePaths.Add(docFilePathB); FileInfo info = new FileInfo(docFilePathB); ListViewItem lvi = new ListViewItem((i + 1).ToString()); lvi.SubItems.Add(model.Doc[i].Filename); lvi.SubItems.Add(Math.Ceiling(info.Length / 1024.0) >= 1024 ? ((Math.Ceiling(info.Length / 1024.0) / 1024).ToString("F2") + "MB") : (Math.Ceiling(info.Length / 1024.0).ToString("F2") + "KB")); lvi.SubItems.Add("0.00%"); lvi.SubItems.Add(FileUpLoadModel.FileUpLoadStateEnum.未开始.ToString()); lvi.BackColor = i % 2 == 0 ? Color.FromArgb(250, 250, 250) : Color.FromArgb(247, 247, 247); lvi.Tag = model.Doc[i]; lvFileInfo.Items.Add(lvi); } else { MessageBox.Show("文件不完整!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtBomFilePath.Text = string.Empty; txtBomFilePath.Focus(); return; } } } else { MessageBox.Show("到底是新建设计项目呢还是设计更改申请呢?\n不懂、不懂...", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); btnUpLoad.Enabled = false; } } }
/// <summary> /// 读取XML文件 -- 设计更改申请 /// </summary> /// <param name="strXmlFilePath"></param> /// <returns></returns> public RootChangeModel GetDocDataByLocalXML(string strXmlFilePath) { try { //xml文件读取的对象 RootChangeModel result = new RootChangeModel(); //文档对象 XmlDocument doc = new XmlDocument(); //加载xml文件 //doc.Load(strXmlFilePath); string strXML = File.ReadAllText(strXmlFilePath, Encoding.UTF8); doc.LoadXml(strXML.ToLower()); //查询文档节点 XmlNode changeQNode = doc.SelectSingleNode("/root/changeq"); XmlNode changeNode = doc.SelectSingleNode("/root/change"); //保存文档属性信息 result.ChangeQ = new RootChangeModel.FileModel() { FileName = changeQNode.Attributes["filename"].Value }; result.Change = new RootChangeModel.FileModel() { FileName = changeNode.Attributes["filename"].Value }; //获取Doc节点(Doc节点有多个,所以是集合) XmlNodeList DocNodes = doc.SelectNodes("/root//doc"); //遍历Doc foreach (XmlNode itemA in DocNodes) { //实例化一个Doc对象 RootChangeModel.DocModel docModel = new RootChangeModel.DocModel() { Code = itemA.Attributes["code"].Value, Code1 = itemA.Attributes["code1"].Value, Name = itemA.Attributes["name"].Value, Version = itemA.Attributes["version"].Value, Gcname = itemA.Attributes["gcname"].Value, Page = itemA.Attributes["page"].Value, Totpage = itemA.Attributes["totpage"].Value, Filename = itemA.Attributes["filename"].Value, Part = new List <RootChangeModel.DocModel.PartModel>() }; //获取所有Part零件节点 XmlNodeList PartNodes = itemA.SelectNodes("part"); //遍历产品文档 foreach (XmlNode itemB1 in PartNodes) { docModel.Part.Add(new RootChangeModel.DocModel.PartModel() { Code = itemB1.Attributes["code"].Value, Code1 = itemB1.Attributes["code1"].Value, Name = itemB1.Attributes["name"].Value, Material = itemB1.Attributes["material"].Value, Sigweight = itemB1.Attributes["sigweight"].Value, TotWeight = itemB1.Attributes["totweight"].Value, Remark = itemB1.Attributes["remark"].Value }); } //将当前Doc信息添加到xml对象的Doc里 result.Doc.Add(docModel); } btnUpLoad.Enabled = true; btnUpLoad.Focus(); return(result); } catch (Exception ex) { string err = ex.ToString(); MessageBox.Show("XML文件错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); btnUpLoad.Enabled = false; return(new RootChangeModel()); } }