private void BtnBrowse_Click(object sender, EventArgs e) { DialogResult result = folderBrowserDialog1.ShowDialog(); if (result == DialogResult.OK) { FolderPath = folderBrowserDialog1.SelectedPath; TbFolderPath.Text = FolderPath; } } private void BtnSubmit_Click(object sender, EventArgs e) { DirectoryInfo Dirinfo = new DirectoryInfo(FolderPath); foreach (var file in Dirinfo.GetFiles("*.sgm")) { XmlDocument doc = new XmlDocument(); doc.Load(FolderPath+"\\"+ file.Name); List<string> Correct = new List<string>(); List<string> Wrong = new List<string>(); XmlNode root = doc.FirstChild; if (root.HasChildNodes) { // get all nodes with tag name "Level" foreach (XmlNode node in root.ChildNodes) { Correct.Clear(); Wrong.Clear(); foreach (XmlNode node1 in node.ChildNodes) { if (node1.Name == "correct") { Correct.Add(node1.InnerText); } if (node1.Name == "wrong") { Wrong.Add(node1.InnerText); } } if (Correct.Count > 0) { XmlNode CorrectMain = doc.CreateElement("CorrectAnswers"); CorrectMain.InnerText = "Correct Answer Sequence"; foreach (string correct in Correct) { XmlNode ChildNode = doc.CreateElement("CorrectAns"); ChildNode.InnerText = correct; CorrectMain.AppendChild(ChildNode); } node.AppendChild(CorrectMain); doc.Save(FolderPath + "\\" + file.Name); } if (Wrong.Count > 0) { XmlNode CorrectMain = doc.CreateElement("WrongAnswers"); CorrectMain.InnerText = "Wrong Answer Sequence"; foreach (string wrong in Wrong) { XmlNode ChildNode = doc.CreateElement("WrongAns"); ChildNode.InnerText = wrong; CorrectMain.AppendChild(ChildNode); } node.AppendChild(CorrectMain); doc.Save(FolderPath + "\\" + file.Name); } } } } TbFolderPath.Text = string.Empty; FolderPath = string.Empty; MessageBox.Show("Completed"); }</string></string></string></string>
if (root.HasChildNodes) { foreach (XmlNode node in root.ChildNodes) { //your code... if (Correct.Count > 0) { //your code... //don't save the document here.... //doc.Save(FolderPath + "\\" + file.Name); } if (Wrong.Count > 0) { //your code... //don't save the document here.... //doc.Save(FolderPath + "\\" + file.Name); } // save after all correcr and wrong child node added doc.Save(FolderPath + "\\" + file.Name); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)