首页 > 编程 > .NET > 正文

asp.net 操作xml

2024-07-10 13:09:15
字体:
来源:转载
供稿:网友

asp.net 对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的样式:
<?xml version="1.0" encoding="gb2312"?>
<user>
  <person>
  </person>
  <person name="风拉" sex="男" age="25">
    <pass>123</pass>
    <address>大明</address>
  </person>
  <person name="风云" sex="女" age="25">
    <pass>123</pass>
    <address>昆明</address>
  </person>
</user>



收集最实用的网页特效代码!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表