现在要实现的功能就是如<Employees>
  <Node name="dd" gangwei="dd" time="2011/4/27 9:43:34" /> 
  </Employees>这样的,当我C#后台重新调用函数的时候,应该生成
<Employees>
  <Node name="dd" gangwei="dd" time="2011/4/27 9:43:34" /> 
 <Node name="dd" gangwei="dd" time="2011/4/27 9:43:34" /> 
  </Employees>这样的效果,而不是把第一个节点Node给替换掉,求教各位高手,该怎么实现呢?
后台代码贴出,望指教,在线等。
protected void AddXml()
    {
        xmldoc = new XmlDocument();
        //加入XML的声明段落<?xml version="1.0" encoding="gb2312"?>
        XmlDeclaration xmldecl;
        xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
        xmldoc.AppendChild(xmldecl);
        //加入一个根元素
        xmlelem = xmldoc.CreateElement("", "Employees", "");
        xmldoc.AppendChild(xmlelem);
        //加入另外一个元素        XmlNode root = xmldoc.SelectSingleNode("Employees");//查找<Employees>
        XmlElement xe1 = xmldoc.CreateElement("Node");//创建一个<Node>
        xe1.SetAttribute("name", Request["textname"] + "");//设置该节点属性
        xe1.SetAttribute("gangwei", Request["textfirst"] + "");//设置该节点属性
        xe1.SetAttribute("time",DateTime.Now+"");
                root.AppendChild(xe1);//添加到<Employees>节点中 
        //保存创建好的XML文档
        xmldoc.Save(Server.MapPath("Recruitment.xml"));
    }

解决方案 »

  1.   

    你那个代码不是每次都新建一个XML吗?不是被你替换了那个节点,而是被你新建了,你只要在创建前先尝试读取下Recruitment.xml,如果存在,就是用Recruitment.xml创建一个XmlDocument
      

  2.   

    能详细点吗?第一次接触XML,还不懂,
      

  3.   

    如果是那样的话,就这样写
    if (File.Exists(Server.MapPath("Recruitment.xml")))
                {
                    xmldoc.Load(Server.MapPath("Recruitment.xml"));
                }
                else
                {
                    xmldoc = new XmlDocument();
                    //加入XML的声明段落<?xml version="1.0" encoding="gb2312"?>
                    XmlDeclaration xmldecl;
                    xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
                    xmldoc.AppendChild(xmldecl);            }这样,前面先去判断是否存在该文件了。
      

  4.   

     
    XmlDocument xmldoc = new XmlDocument();
    //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
    XmlDeclaration xmldecl;
    xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
    xmldoc.AppendChild(xmldecl);
    XmlElement rootElement = null;
    XmlElement parentElement = null;
    XmlNode rootNode = null;
    rootElement = xmldoc.CreateElement("Employees", "gb2312", "");
    xmldoc.AppendChild(rootElement);
    rootNode = xmldoc.SelectSingleNode("Employees");
    parentElement = xmldoc.CreateElement("Node"); 
    parentElement.SetAttribute("name", "dd");
    parentElement.SetAttribute("gangwei", "dd");
    rootNode.AppendChild(parentElement);
    xmldoc.Save("Recruitment.xml");
      

  5.   

    来晚了哦~嘿嘿[align=center]*****************************************
    本内容使用CSDN小秘书回复
    每天回帖即可得10分可用分!
    *****************************************[/align]
      

  6.   

    敲那么多文字,没点奖励分,FUCK YOU!