只获得XML中的两个子级别 [英] Only getting two Child levels in XML

查看:70
本文介绍了只获得XML中的两个子级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是 a nt读取嵌套的xml并在同一个Parent中插入新节点但只有一个孩子得到答案



Hi

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

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

}


这篇关于只获得XML中的两个子级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆