Click here to Skip to main content
15,881,635 members
Please Sign up or sign in to vote.
1.67/5 (2 votes)
See more:
Hi

I want to read nested xml and insert new node in same Parent but only one child is getting as answer

C#
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>
Posted
Updated 2-Jun-14 15:20pm
v3
Comments
Sergey Alexandrovich Kryukov 2-Jun-14 21:02pm    
It's bad unsupportable code anyway, due to its ad-hoc nature and immediate constants like "WrongAns", "WrongAnswers", "\\", "*.sgm", etc. This is the never-ending source of bugs. In all cases, you need to show more detail: original XML, XML you obtained, XML you wanted to get, explanation of what part is wrong, and so on.
—SA

1 solution

C#
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);
  }

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900