// Save xml private void btnSaveGradeConfigFile_Click(object sender, EventArgs e) { // if some data wrong will tip. if (_dtbColumns.Rows.Count < 1 || _dtbRows.Rows.Count < 1 || _dtbPoints.Rows.Count < 1 || _dtbGrades.Rows.Count < 1) { MessageBox.Show("Input has null value."); return; } DataHelper dh = new DataHelper(); if (dh.HasNull(_dtbColumns) || dh.HasNull(_dtbRows) || dh.HasNull(_dtbPoints) || dh.HasNull(_dtbGrades)) { MessageBox.Show("Input has null value."); return; } // Save default config file to system config ConfigHelper ch = new ConfigHelper(); if (String.IsNullOrEmpty(cmbConfig.Text)) { cmbConfig.Text = DateTime.Now.ToShortDateString(); } // if save error break; if (!ch.SaveGradeSetupConfigFile(cmbConfig.Text.Trim())) { MessageBox.Show("File name is empty."); return; } // ROI-Setting //----------------------------------------------------------------------------------// // initialize grade xml sechma string sechma_path = PathHelper.SystemConfigFolder + "grade_sechma.xml"; XmlDocument document = new XmlDocument(); document.Load(sechma_path); XPathNavigator navigator = document.CreateNavigator(); // save roi mode RadioButton[] rdos = { rdoNoRoi, rdoSymmetrical }; foreach (RadioButton rdo in rdos) { if (rdo.Checked) { navigator.SelectSingleNode("//roi/mode").SetValue(rdo.Text.Trim()); } } // Save roi columns , rows size navigator.SelectSingleNode("//roi/columns/size").SetValue(_dtbColumns.Rows.Count.ToString()); navigator.SelectSingleNode("//roi/rows/size").SetValue(_dtbRows.Rows.Count.ToString()); // save roi data (start, end) of columns , rows. // Remove old columns for add new record if (navigator.Select("//roi/columns/column").Count > 0) { XPathNavigator first = navigator.SelectSingleNode("//roi/columns/column[1]"); XPathNavigator last = navigator.SelectSingleNode("//roi/columns/column[last()]"); navigator.MoveTo(first); navigator.DeleteRange(last); } // save _dgvColumns data to xml for (int i = 0; i < _dtbColumns.Rows.Count; i++) { string name = _dtbColumns.Rows[i]["Name"].ToString(); string start = _dtbColumns.Rows[i]["Start"].ToString(); string end = _dtbColumns.Rows[i]["End"].ToString(); navigator.SelectSingleNode("//roi/columns").AppendChildElement(string.Empty, "column", string.Empty, null); // Move to last column element and add name, start, end value. navigator.SelectSingleNode("//roi/columns/column[last()]").AppendChildElement(string.Empty, "name", string.Empty, name); navigator.SelectSingleNode("//roi/columns/column[last()]").AppendChildElement(string.Empty, "start", string.Empty, start); navigator.SelectSingleNode("//roi/columns/column[last()]").AppendChildElement(string.Empty, "end", string.Empty, end); } // Remove old rows for add new record. if (navigator.Select("//roi/rows/row").Count > 0) { XPathNavigator first = navigator.SelectSingleNode("//roi/rows/row[1]"); XPathNavigator last = navigator.SelectSingleNode("//roi/rows/row[last()]"); navigator.MoveTo(first); navigator.DeleteRange(last); } // save _dtbRows data to xml for (int i = 0; i < _dtbRows.Rows.Count; i++) { string name = _dtbRows.Rows[i]["Name"].ToString(); string start = _dtbRows.Rows[i]["Start"].ToString(); string end = _dtbRows.Rows[i]["End"].ToString(); navigator.SelectSingleNode("//roi/rows").AppendChildElement(string.Empty, "row", string.Empty, null); // Move to last column element and add name, start, end value. navigator.SelectSingleNode("//roi/rows/row[last()]").AppendChildElement(string.Empty, "name", string.Empty, name); navigator.SelectSingleNode("//roi/rows/row[last()]").AppendChildElement(string.Empty, "start", string.Empty, start); navigator.SelectSingleNode("//roi/rows/row[last()]").AppendChildElement(string.Empty, "end", string.Empty, end); } // Grade - Setting //----------------------------------------------------------------------------------// // Points // Save is points enable? navigator.SelectSingleNode("//grade/points/enable").SetValue(chkEnablePonit.Checked.ToString()); // Remove old points/sub_piece for add new record. (_dtbPoints) if (navigator.Select("//grade/points/sub_piece").Count > 0) { XPathNavigator first = navigator.SelectSingleNode("//grade/points/sub_piece[1]"); XPathNavigator last = navigator.SelectSingleNode("//grade/points/sub_piece[last()]"); navigator.MoveTo(first); navigator.DeleteRange(last); } // save _dtbPoints List<string> pointsSubpieces = _pointsSubpieceNames; // prepare flawtype convert dictionary string map_config_path = ch.GetDefaultMapConfigName(); Dictionary<string, int> flawlegends = ch.GetPrevFlawLegendDictionaryID(map_config_path); foreach (string subpieceName in pointsSubpieces) { navigator.SelectSingleNode("//grade/points").AppendChildElement(string.Empty, "sub_piece", string.Empty, null); // Move to last column element and add name value. navigator.SelectSingleNode("//grade/points/sub_piece[last()]").AppendChildElement(string.Empty, "name", string.Empty, subpieceName); string expr = String.Format("SubpieceName='{0}'", subpieceName); // check all same if (chkAllSameOfPoint.Checked) { expr = String.Format("SubpieceName='{0}'", "All"); } DataRow[] drs = _dtbPoints.Select(expr); foreach (DataRow dr in drs) { string className = dr["ClassName"].ToString(); int classID = flawlegends[className]; string score = dr["Score"].ToString(); navigator.SelectSingleNode("//grade/points/sub_piece[last()]").AppendChildElement(string.Empty, "flawtype_score", string.Empty, score); navigator.SelectSingleNode("//grade/points/sub_piece[last()]/flawtype_score[last()]").CreateAttribute(string.Empty, "id", string.Empty, classID.ToString()); } } // Remove old grade(marks)/subpiece for add new record . (_dtbGrades) if (navigator.Select("//grade/marks/sub_piece").Count > 0) { XPathNavigator first = navigator.SelectSingleNode("//grade/marks/sub_piece[1]"); XPathNavigator last = navigator.SelectSingleNode("//grade/marks/sub_piece[last()]"); navigator.MoveTo(first); navigator.DeleteRange(last); } // Marks // Save is grade(marks) enable? navigator.SelectSingleNode("//grade/marks/enable").SetValue(chkEnableGrade.Checked.ToString()); // save _dtbGrades List<string> marksSubpieces = _marksSubpieceNames; foreach (string subpieceName in marksSubpieces) { navigator.SelectSingleNode("//grade/marks").AppendChildElement(string.Empty, "sub_piece", string.Empty, null); //Move to last column element and add name value. navigator.SelectSingleNode("//grade/marks/sub_piece[last()]").AppendChildElement(string.Empty, "name", string.Empty, subpieceName); string expr = String.Format("SubpieceName='{0}'", subpieceName); // check all same if (chkAllSameOfGrade.Checked) { expr = String.Format("SubpieceName='{0}'", "All"); } DataRow[] drs = _dtbGrades.Select(expr); foreach (DataRow dr in drs) { string className = dr["GradeName"].ToString(); string score = dr["Score"].ToString(); navigator.SelectSingleNode("//grade/marks/sub_piece[last()]").AppendChildElement(string.Empty, "mark", string.Empty, score); navigator.SelectSingleNode("//grade/marks/sub_piece[last()]/mark[last()]").CreateAttribute(string.Empty, "id", string.Empty, className); } } // Pass or Fail // Save filter score is enable? navigator.SelectSingleNode("//grade/pass_fail/enable").SetValue(chkEnablePFS.Checked.ToString()); int filterScore = int.TryParse(txtFilterScore.Text, out filterScore) ? filterScore : 0; navigator.SelectSingleNode("//grade/pass_fail/score").SetValue(filterScore.ToString()); // Finish Save //----------------------------------------------------------------------------------// string grade_path = PathHelper.GradeConfigFolder + cmbConfig.Text.Trim() + ".xml"; try { document.Save(grade_path); // Re binding cmbMapConfigName datasource List<string> graeConfigs = new List<string>(); DirectoryInfo dirInfo = new DirectoryInfo(PathHelper.GradeConfigFolder); FileInfo[] files = dirInfo.GetFiles("*.xml"); foreach (FileInfo file in files) { graeConfigs.Add(file.Name.ToString().Substring(0, file.Name.ToString().LastIndexOf("."))); } // Binding datasource for cmbMapConfigName and set default value. cmbConfig.DataSource = graeConfigs; cmbConfig.SelectedItem = ch.GetDefaultGradeConfigName().Trim(); MessageBox.Show("Success."); } catch { MessageBox.Show("Fail."); } // Refresh datas // Refresh dgvPoint datasource _dtbPoints.Clear(); DataTable tmpPoints = ch.GetDataTabledgvPoints(cmbConfig.SelectedItem.ToString().Trim()); foreach (DataRow dr in tmpPoints.Rows) { DataRow d = _dtbPoints.NewRow(); d["SubpieceName"] = dr["SubpieceName"]; d["ClassName"] = dr["ClassName"]; d["Score"] = dr["Score"]; _dtbPoints.Rows.Add(d); } DataView dvPoints = _dtbPoints.DefaultView; dvPoints.RowFilter = String.Format("SubpieceName='{0}'", cmbSubPoints.SelectedItem.ToString().Trim()); tmpPoints.Dispose(); // Refresh dgvGrade datasource _dtbGrades.Clear(); DataTable tmpGrades = ch.GetDataTabledgvGrade(cmbConfig.SelectedItem.ToString().Trim()); foreach (DataRow dr in tmpGrades.Rows) { DataRow d = _dtbGrades.NewRow(); d["SubpieceName"] = dr["SubpieceName"]; d["GradeName"] = dr["GradeName"]; d["Score"] = dr["Score"]; _dtbGrades.Rows.Add(d); } DataView dvGrade = _dtbGrades.DefaultView; dvGrade.RowFilter = String.Format("SubpieceName='{0}'", cmbSubMarks.SelectedItem.ToString().Trim()); tmpGrades.Dispose(); }