/// <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="filePath"></param> /// <returns></returns> public RootNewModel GetProductDataByLocalXML(string strXmlFilePath) { try { //xml文件读取的对象 RootNewModel result = new RootNewModel(); //文档对象 XmlDocument doc = new XmlDocument(); //加载xml文件 //doc.Load(strXmlFilePath); string strXML = File.ReadAllText(strXmlFilePath, Encoding.UTF8); int explain_S = strXML.IndexOf("<?"); int explain_E = strXML.IndexOf("?>"); string strExplain = string.Empty; if (!explain_S.Equals(-1)) { strExplain = strXML.Substring(explain_S, explain_E + 2); strXML = strXML.Replace(strExplain, strExplain.ToLower()); } doc.LoadXml(strXML); //查root根节点 XmlNode rootNode = GetNode(doc, "/ROOT", "找不到【ROOT】!"); //查询项目节点(项目有一个,所以是对象) XmlNode projectNode = GetNode(doc, "/ROOT/PROJECT", "找不到【PROJECT】!"); //保存项目属性信息 result.Project.Projectid = GetAttrValue(projectNode, "PROJECTID"); result.Project.Projectname = GetAttrValue(projectNode, "PROJECTNAME"); //获取产品节点(产品有多个,所以是集合) XmlNodeList ProductNodes = GetNodeList(doc, "/ROOT//PRODUCT", "找不到【PRODUCT】!"); //遍历产品 foreach (XmlNode itemA in ProductNodes) { //实例化一个产品对象 RootNewModel.ProductModel productModel = new RootNewModel.ProductModel(); //获取这个产品下的所有DOC文档节点 XmlNodeList DOCNodesA = GetNodeList(itemA, "DOC"); //获取这个产品下的所有Part零件节点 XmlNodeList PartNodesA = GetNodeList(itemA, "PART"); //获取产品信息(属性值) productModel.Code = GetAttrValue(itemA, "CODE"); productModel.Code1 = GetAttrValue(itemA, "CODE1"); productModel.Version = GetAttrValue(itemA, "VERSION"); productModel.Name = GetAttrValue(itemA, "NAME"); productModel.Quantity = GetAttrValue(itemA, "QUANTITY"); productModel.Material = GetAttrValue(itemA, "MATERIAL"); productModel.Sigweight = GetAttrValue(itemA, "SIGWEIGHT"); productModel.TotWeight = GetAttrValue(itemA, "TOTWEIGHT"); productModel.Remark = GetAttrValue(itemA, "REMARK"); productModel.FaHCode = GetAttrValue(itemA, "FAHCODE"); //遍历产品文档 foreach (XmlNode itemB1 in DOCNodesA) { RootNewModel.ProductModel.DocModel docModel = new RootNewModel.ProductModel.DocModel() { Code = GetAttrValue(itemB1, "CODE"), Code1 = GetAttrValue(itemB1, "CODE1"), Name = GetAttrValue(itemB1, "NAME"), Version = GetAttrValue(itemB1, "VERSION"), Gcname = GetAttrValue(itemB1, "GCNAME"), Page = GetAttrValue(itemB1, "PAGE"), Totpage = GetAttrValue(itemB1, "TOTPAGE"), Filename = GetAttrValue(itemB1, "FILENAME") }; productModel.Doc.Add(docModel); docModels.Add(docModel); } //遍历所有一级Part零件 foreach (XmlNode itemB2 in PartNodesA) { //当前零件的父级节点,即当前产品节点 XmlNode productNode = itemB2.ParentNode; //通过递归方法获取当前零件下的所有Part零件以及DOC文档 productModel.Part.Add(GetPart(itemB2)); } //将当前产品信息添加到xml对象的产品里 result.Product.Add(productModel); } btnUpLoad.Enabled = true; btnUpLoad.Focus(); return(result); } catch (Exception ex) { MessageBox.Show("XML文件错误!\n详情:" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); btnUpLoad.Enabled = false; docModels.Clear(); return(new RootNewModel()); } }