xml中的常用操作示例

 

 

XmlDocument xmlDoc;
            xmlDoc = new XmlDocument();
            xmlDoc.Load(Application.StartupPath+"\\pointConfig.xml");
            XmlNode xmldocSelect = xmlDoc.SelectSingleNode("root");//xml文件的根结点是user
            xmldocSelect = xmldocSelect.SelectNodes("pointList")[0];
            XmlElement el = xmlDoc.CreateElement("point");//添加person节点
            el.SetAttribute("index", "1");//添加属性名,及属性值
            el.SetAttribute("trackNo", "1");


            XmlElement xesub1 = xmlDoc.CreateElement("arriveTime");
            xesub1.InnerText = "2012-05-05 13:05:00";
            el.AppendChild(xesub1);
            XmlElement xesub2 = xmlDoc.CreateElement("lat");
            xesub2.InnerText = "133";
            el.AppendChild(xesub2);
            XmlElement xesub3 = xmlDoc.CreateElement("lon");
            xesub3.InnerText = "39";
            el.AppendChild(xesub3);
            XmlElement xesub4 = xmlDoc.CreateElement("ico");
            xesub4.SetAttribute("defaultDisply", "true");
            xesub4.InnerText = @"http://localhost/a.gif";
            el.AppendChild(xesub4);

            XmlElement xesub5 = xmlDoc.CreateElement("text");
            xesub5.InnerText = "cccc";
            xesub5.SetAttribute("defaultDisply", "true");
            el.AppendChild(xesub5);
            XmlElement xesub6 = xmlDoc.CreateElement("tag");
            xesub6.SetAttribute("action", "click");

            el.AppendChild(xesub4);
            XmlElement e2 = (XmlElement)el.Clone();
            e2.SelectNodes("lon")[0].InnerText="134";
            e2.SetAttribute("index", "2");
            xmldocSelect.AppendChild(el);//将节点person加到根结点xmldocSelect下
            xmldocSelect.AppendChild(e2);
            xmlDoc.Save("trytry.xml");//保存,这个十分重要,否则没有数据

 

 

private XmlDataDocument xmlDoc;//声明全局变量xmlDoc,类型是XmlDataDocument
     protected void Page_Load(object sender, EventArgs e)
     {
 
    }
 
//声明加载xml文件的方法
     private void xmlLoad()
     {
         xmlDoc = new XmlDataDocument();
         xmlDoc.Load(Server.MapPath("trytry.xml"));//文件名是trytry.xml
     }
 
//点击添加按钮,添加数据
     protected void ButtonAdd_Click(object sender, EventArgs e)
     {
         xmlLoad();//调用加载xml文件的方法
         XmlNode xmldocSelect = xmlDoc.SelectSingleNode("user");//xml文件的根结点是user
 
        XmlElement el = xmlDoc.CreateElement ("person");//添加person节点
         el.SetAttribute("name", "云云");//添加属性名,及属性值
         el.SetAttribute("sex", "女");
         el.SetAttribute("age", "25");
 
        XmlElement xesub1 = xmlDoc.CreateElement("pass");//添加person节点的子节点
         xesub1.InnerText = "123";//节点pass下的值是123
         el.AppendChild(xesub1);//将节点pass添加到父节点person下
         XmlElement xesub2 = xmlDoc.CreateElement("address");
         xesub2.InnerText = "中国西安";
         el.AppendChild(xesub2);
 
        xmldocSelect.AppendChild(el);//将节点person加到根结点xmldocSelect下
         xmlDoc.Save(Server.MapPath("trytry.xml"));//保存,这个十分重要,否则没有数据
 
    }
 
//点击修改按钮,修改数据
     protected void ButtonUpdata_Click(object sender, EventArgs e)
     {
         xmlLoad();//调用加载xml文件的方法
         XmlNodeList list = xmlDoc.SelectSingleNode("user").ChildNodes;//获得根节点user下的所有子节点
 
 
 
//遍历所有子节点
 
        foreach (XmlNode node in list)
         {
             XmlElement xe = (XmlElement)node;//把节点node转换成XmlElement型
             if (xe.GetAttribute("name") == "云云")//如果节点的属性name的值是"云云"
             {
                 xe.SetAttribute("name","哈哈");//把节点的name属性改为"哈哈"
             }
             XmlNodeList sublist = xe.ChildNodes;//再获得该节点的子节点
             foreach (XmlNode subnode in sublist)
             {
                 XmlElement subxe = (XmlElement)subnode ;
                 if (subxe.Name == "address")//如果节点是"address"
                 {
                     if (subxe.InnerText == "中国西安")//如果该节点下的值是"中国西安"
                         subxe.InnerText = "中国三河";
                     break;
                 }
             }
         }
         xmlDoc.Save(Server.MapPath("trytry.xml"));
     }
 
//点击删除按钮,删除数据
     protected void ButtonDele_Click(object sender, EventArgs e)
     {
         xmlLoad();
         XmlNodeList list = xmlDoc.SelectSingleNode("user").ChildNodes;
         foreach (XmlNode node in list)
         {
             XmlElement xe = (XmlElement)node;
             if (xe.GetAttribute("name") == "哈哈")
             {
                 xe.RemoveAttribute("name");//删除属性
             }
             else
                 xe.RemoveAll();//删除节点
         }
 
        xmlDoc.Save(Server.MapPath("trytry.xml"));
      
     }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值