黑马程序员--XML添加读取删除的实现

------------------ Windows Phones 7手机开发.Net培训、期待与您交流! ----------------------

XML数据:

 

添加功能代码:

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(".\\XMLL.xml");

            //添加IP地址
            XmlElement ele = xmlDoc.CreateElement("IP地址");
            XmlText text = xmlDoc.CreateTextNode(this.txtAddIP.Text);
            //添加MAC地址
            XmlElement ele1 = xmlDoc.CreateElement("MAC地址");
            XmlText text1 = xmlDoc.CreateTextNode(this.txtAddMAC.Text);
            //添加元素内容


            //添加文件的节点msgrecord
            XmlNode newElem = xmlDoc.CreateNode("element", "用户信息", "");
            //在节点中添加元素
            newElem.AppendChild(ele);
            newElem.LastChild.AppendChild(text);
            newElem.AppendChild(ele1);
            newElem.LastChild.AppendChild(text1);

            //将节点添加到文档中
            XmlElement root = xmlDoc.DocumentElement;
            root.AppendChild(newElem);
            //保存所有修改
            xmlDoc.Save(".\\XMLL.xml");

 

读取功能代码:

            ArrayList array1 = new ArrayList();
            ArrayList array2 = new ArrayList();
            //创建一个文档对象
            XmlDocument doc = new XmlDocument();
            //加载xml文件
            doc.Load(".\\XMLL.xml");
            //获得文档的根节点。
            XmlElement rootNode = doc.DocumentElement;
            //遍历根节点

            foreach (XmlNode nodes in rootNode)
            {
                if (nodes.Name == "用户信息")
                {
                    foreach (XmlNode node in nodes)
                    {
                        switch (node.Name)
                        {                  
                            case "IP地址":
                                array1.Add(node.InnerText);
                                break;
                            case "MAC地址":
                                array2.Add(node.InnerText); 
                                break;
                        }
                    } 

                }
            }

 

 

删除功能代码:

    string theTitle="试用 FCKeditor 2.0 RC3";
    string theRq="6.13";

    // 创建一个XmlDocument对象
    string xmlPath=Server.MapPath("blog.xml");
    XmlDocument xmlDoc=new XmlDocument();
    // 加载指定的XML数据
    xmlDoc.Load(xmlPath);
    // 选择匹配 XPath 表达式的第一个 XmlNode
    XmlNode root=xmlDoc.SelectSingleNode("data");
    // 获取节点的所有子节点
    XmlNodeList xnList=root.ChildNodes;
    foreach(XmlNode xn in xnList){
      if(xn.SelectSingleNode("title").InnerText==theTitle && xn.SelectSingleNode("rq").InnerText==theRq){
        root.RemoveChild(xn);// 移除指定的子节点
        xmlDoc.Save(xmlPath);
        break;
      }
    }

------------------ Windows Phones 7手机开发.Net培训、期待与您交流! ----------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值