1.首先新建一个xml文件(Root是我写上的)

c#操作xml增删改查

2.

c#操作xml增删改查

3.直接上代码,更直观

(1)初始化xml

  

  /// <summary>
        /// 初始化xml
        /// </summary>
        public void LoadXml()
        {
            xmlDoc = new XmlDocument();
            xmlDoc.Load(Server.MapPath("../wx.xml"));
        }

(2)添加节点

 /// <summary>
        /// 向xml中添加数据
        /// </summary>
        public void AddElement(string FromUserName)
        {
            LoadXml();
            XmlNode xmldocSelect = xmlDoc.SelectSingleNode("Root");//查找节点
            XmlElement el = xmlDoc.CreateElement("Person");  //添加person节点  
            el.SetAttribute("name", FromUserName);  //添加person节点的属性"name" 
            el.SetAttribute("time", DateTime.Now.ToString());
            xmldocSelect.AppendChild(el);
            xmlDoc.Save(Server.MapPath("../wx.xml")); 
        }
View Code

相关文章: