一聚教程网:一个值得你收藏的教程网站

热门教程

xml文件的读写,添加,修改,删除操作

时间:2022-06-25 06:35:52 编辑:袖梨 来源:一聚教程网

xml文件的读写,添加,修改,删除操作
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.xml;
private xmldocument xmldoc;

        //load xml file
        private void loadxml()
        {
            xmldoc=new xmldocument();
            xmldoc.load(server.mappath("user.xml"));
        }

        //添加节点
        private void addelement()
        {
           
            loadxml();

            xmlnode xmldocselect=xmldoc.selectsinglenode("user");
           
            xmlelement el=xmldoc.createelement("person"); //添加person节点
            el.setattribute("name","风云"); //添加person节点的属性"name"
            el.setattribute("sex","女");   //添加person节点的属性 "sex"
            el.setattribute("age","25");   //添加person节点的属性 "age"
           
             xmlelement xesub1=xmldoc.createelement("pass"); //添加person节点的里的节点
             xesub1.innertext="123";//设置文本节点
             el.appendchild(xesub1);
             xmlelement xesub2=xmldoc.createelement("address");
             xesub2.innertext="昆明";//设置文本节点
             el.appendchild(xesub2);

            xmldocselect.appendchild(el);
            xmldoc.save(server.mappath("user.xml"));
        }

        //修改节点
        private void updateelement()
        {
            loadxml();
            xmlnodelist nodelist=xmldoc.selectsinglenode("user").childnodes;//获取bookstore节点的所有子节点
            foreach(xmlnode xn in nodelist)//遍历所有子节点
            {
                xmlelement xe=(xmlelement)xn;//将子节点类型转换为xmlelement类型
                if(xe.getattribute("name")=="风云")//如果name属性值为“风云”
                {
                    xe.setattribute("name","发明");

                   
                    //如果下面有子节点在下走
                     xmlnodelist nls=xe.childnodes;//继续获取xe子节点的所有子节点
                    foreach(xmlnode xn1 in nls)//遍历
                    {
                       xmlelement xe2=(xmlelement)xn1;//转换类型
                        if(xe2.name=="pass")//如果找到
                        {
                            xe2.innertext="66666";//则修改
                            break;
                        }
                    }

                    break;
                }
            }
           xmldoc.save(server.mappath("user.xml"));//保存
        }


        //删出节点
        private void deletenode()
        {
           
            loadxml();
            xmlnodelist xnl=xmldoc.selectsinglenode("user").childnodes;
 
            foreach(xmlnode xn in xnl)
            {
                xmlelement xe=(xmlelement)xn;
               
                if(xe.getattribute("name")=="发明")
                {
                    //xe.removeattribute("name");//删除name属性
                    xe.removeall();//删除该节点的全部内容
                    break;
                }
            }
            xmldoc.save(server.mappath("user.xml"));//保存
        }

        private void showit()
        {
            loadxml();
            xmlnode xn=xmldoc.selectsinglenode("user");
 
            xmlnodelist xnl=xn.childnodes;
  
            foreach(xmlnode xnf in xnl)
            {
                xmlelement xe=(xmlelement)xnf;
//                console.writeline(xe.getattribute("name"));//显示属性值
//                console.writeline(xe.getattribute("sex"));
//
//                xmlnodelist xnf1=xe.childnodes;
//                foreach(xmlnode xn2 in xnf1)
//                {
//                    console.writeline(xn2.innertext);//显示子节点点文本
//                }

            }

        }
   
xml的样式:


 
 

 
    123
   

大明

 
 
    123
   
昆明

 

热门栏目