现在有一个XML文件把它当作模板可向它里面添加节点,但当我点击保存按钮把文件存进数据库后,该怎样还原回模板?(即没有添加节点之前的样子),新手,请说详细点.谢谢 
模板为:    
<root>    
      <element Name="F2" Description="记实测评" Score="5">    
        <element2 Name="F21" DesCription="必修项" Score="5">    
          <element3 Name="F211" Description="上课出勤情况" Score="0">    
          </element3>    
          <element3 Name="F212" Description="班集体活动参与情况" Score="0">    
          </element3>    
      <element2 Name="F22" DesCription="选修与附加项" Score="0">    
现在要添加节点:    
<root>    
      <element Name="F2" Description="记实测评" Score="10">    
        <element2 Name="F21" DesCription="必修项" Score="0">    
          <element3 Name="F211" Description="上课出勤情况" Score="0">    
          </element3>    
          <element3 Name="F212" Description="班集体活动参与情况" Score="0">    
          </element3>    
      <element2 Name="F22" DesCription="选修与附加项" Score="10">    
       <element3 Name="F221" Description="全国三好学生" ContentID="1" Score="6" />  
         <element3 Name="F221" Description="省级三好学生" ContentID="1" Score="4" />   
       </element2>    
      </element>    
</root>   

解决方案 »

  1.   


           XmlDataDocument XD = new XmlDataDocument();
            XD.Load(Server.MapPath("模板.xml"));
            //....XML操作。
            //然后保存的时候
            XD.Save(Server.MapPath("结果.xml"));
      

  2.   

    我的意思不是这样的,是怎样将"结果.xml"还原为没有添加节点之前的模板?
      

  3.   

    DiffGram格式有回滚功能,楼主查查相关资料
      

  4.   

    具体能不能代码写一下哦,备份是用Clone吗?我怎么试了不成功
      

  5.   

    直接用clone不行 须通过加载复制节点的xml方式复制文件
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("model.xml");
            XmlDocument tempXml = new XmlDocument();
            XmlNode node = xmlDoc.CloneNode(true);
            tempXml.LoadXml(node.InnerXml);        
            //……对tempXml的操作
            //……
            //tempXml.Save("C:\\tempModel.xml"); //可以保存来看看
      

  6.   

    刚才试了,报错路径中具有非法字符
    XmlDocument BackUpXml = new XmlDocument();//BackUpXml备份文件
            BackUpXml.Load(Server.MapPath("~/XML/BackUp.xml"));
            XmlDocument xmlDoc = new XmlDocument();
            XmlNode node = BackUpXml.CloneNode(true);
            xmlDoc.Load(node.InnerXml);  //这里提示System.ArgumentException: 路径中具有非法字符 
      

  7.   


    那应该是你的BackUpXml.Load(Server.MapPath("~/XML/BackUp.xml")); 这句的问题
    可以直接写BackUpXml.Load(@"~/XML/BackUp.xml"); 
      

  8.   

    BackUpXml.Load(Server.MapPath("~/XML/BackUp.xml")); 没有错的,因为之前在相同路径下我做过没错.
    是不是(node.InnerXml)这里有问题呢?
      

  9.   

    为了方便大家给我找原因,我把代码帖出来,这问题捆扰我好多天了,希望能在大家帮助下早点解决   
     protected void Button1_Click(object sender, EventArgs e)
        {
            XmlDocument BackUpXml = new XmlDocument();//BackUpXml备份文件
            BackUpXml.Load(Server.MapPath("~/XML/BackUp.xml"));
            XmlDocument xmlDoc = new XmlDocument();
            XmlNode node = BackUpXml.CloneNode(true);
            xmlDoc.Load(node.InnerXml);  //这里提示System.ArgumentException: 路径中具有非法字符   
            XmlNodeList xnl = xmlDoc.SelectSingleNode("root").ChildNodes;
            int attr = 0;
            int attr2 = 0;
            int f1 = 0;
            string sum;
            int allvalue = 0;
            foreach (XmlNode xn in xnl)//遍历XML中属于root的每个节点
            {
                XmlElement xe = (XmlElement)xn;
                XmlNodeList xnl1 = xe.ChildNodes;
                foreach (XmlNode xn1 in xnl1)//遍历XML中属于F2的每个节点 
                {
                    XmlElement xe2 = (XmlElement)xn1;
                    XmlNodeList xnl2 = xe2.ChildNodes;
                    if (xe2.Attributes["Name"].Value != "F22")
                    {
                        continue;
                    }
                    else
                    {
                        DropDownList aDropdown1 = (DropDownList)GridView2.Rows[0].Cells[1].FindControl("DropDownList1");
                        DropDownList aDropdown2 = (DropDownList)GridView2.Rows[0].Cells[2].FindControl("DropDownList2");
                        int f = 0;
                        string aRank = aDropdown1.SelectedItem.ToString();
                        string aContent = aDropdown2.SelectedItem.ToString();
                        int aRankID;
                        int aContentID;
                        aRankID = Convert.ToInt32(aDropdown1.SelectedValue);
                        aContentID = Convert.ToInt32(aDropdown2.SelectedValue);
                        XmlElement xe3 = xmlDoc.CreateElement("element3");
                        xe3.SetAttribute("Name", "F221");
                        xe3.SetAttribute("Description", (aRank + aContent));
                        xe3.SetAttribute("ContentID", aContentID.ToString());
                        SqlConnection con = new SqlConnection("Data Source=DONG;Initial Catalog=eSchool_SynthesisEvaluation;User ID=sa;Password=123456");                    SqlCommand cmd = new SqlCommand("select Mark From ContentRank where RankID=@aRankID and ContentID=@aContentID", con);
                        con.Open();
                        cmd.Parameters.AddWithValue("@aRankID", aRankID);
                        cmd.Parameters.AddWithValue("@aContentID", aContentID);
                        cmd.ExecuteNonQuery();
                        xe3.SetAttribute("Score", cmd.ExecuteScalar().ToString());
                        if (xn1.ChildNodes.Count != 0)
                        {
                            bool flag = false;
                            foreach (XmlNode xn2 in xnl2)
                            {
                                XmlElement xe4 = (XmlElement)xn2;
                                string aDescription = xe4.Attributes["Description"].Value;
                                if (aDescription == (aRank + aContent))
                                {
                                    //在这里提示{不能重复添加该项}
                                    flag = true;
                                    break;                            }                            //else
                                //{
                                //    ((XmlElement)xn1).AppendChild(xe3);
                                //    TextBox1.Text += (aRank + aContent);
                                //}
                            }
                            if (flag == false)
                            {
                                ((XmlElement)xn1).AppendChild(xe3);
                                TextBox1.Text += (aRank + aContent);
                            }
                        }
                        else
                        {
                            ((XmlElement)xn1).AppendChild(xe3);
                            TextBox1.Text += (aRank + aContent);
                        }
                    }
                }
                foreach (XmlNode xn1 in xnl1)
                {
                    //定义一个字典类
                    IDictionary<string, int> dictionary = new Dictionary<string, int>();
                    int avalue = 0;
                    XmlElement xe2 = (XmlElement)xn1;
                    XmlNodeList xnl2 = xe2.ChildNodes;
                    foreach (XmlNode xn2 in xnl2)
                    {
                        XmlElement xe3 = (XmlElement)xn2;
                        //int aContentID = Convert.ToInt32(xn2.Attributes["ContentID"].Value);
                        int score = int.Parse(xe3.GetAttribute("Score"));
                        string id = xe3.GetAttribute("ContentID");
                        if (dictionary.ContainsKey(id))
                        {
                            if (dictionary[id] < score)
                            {
                                dictionary[id] = score;
                            }
                        }
                        else
                        {
                            dictionary[id] = score;                    }
                        //avalue += dictionary[id];                    //int score = int.Parse(element.GetAttribute("Score"));
                        //avalue += Convert.ToInt32(xn2.Attributes["Score"].Value);                }
                    foreach (string key in dictionary.Keys)
                    {
                        avalue += dictionary[key];
                    }
                    xe2.Attributes["Score"].Value = avalue.ToString();
                    allvalue += Convert.ToInt32(xn1.Attributes["Score"].Value);
                }
                xe.Attributes["Score"].Value = allvalue.ToString();
            }        xmlDoc.Save(Server.MapPath("~/XML/NewXML.xml"));
        }
      

  10.   


    和具体工作流程无关 我这里只是说复制xml文件的方法 应该没有问题的(在我这里运行正常) 
    或许是你的xml数据的原因
      

  11.   

    你的XML文件格式有问题,你确定你那文件能正确打开为XML格式吗?
      

  12.   

    xml没有问题的,我没有用副本之前一切是正常的,就是添加节点后不能恢复为XML模板