• C#简单操作XML文件的增、删、改、查



      

      请老鸟屏蔽以下内容,菜鸟可以继续看,完全是初级入门(说不定算不算入门...)。

      某天突然想到XML是个很强大的东西,但是自己完全不会先关内容,对于XML处于盲目状态。突发奇想,复杂的不会没关系,但是简单的还是必须要会,万一以后要用(比如说来个什么灵活菜单或者保存什么配置节点什么)所以网上看了一点资料,所以完全是稀里糊涂的,只是有点模糊的概念知道sysytem.xml这个命名空间,然后就手动操作了,慢慢的实践,得出自己想要的结果。刚好手里有个xml文件,想到还是不错可以拿来试手。所以很不规范,就当是入门。。。。。。

      xml原来的内容为:

    <?xml version="1.0" encoding="utf-8" ?>
    <leftMenu>
    <directory>
    <item text="搜索地方" href="directoryList.aspx" FunctionType="View"/>
    <item text="添加地方" href="directoryAdd.aspx" operate="add" FunctionType="Add"/>
    <item text="移除地方" href="directoryRemove.aspx" operate="remove" FunctionType="Delete"/>
    </directory>
    <group>
    <item text="搜索组织" href="Grouplist.aspx" FunctionType="View"/>
    <item text="添加组织" href="Groupadd.aspx" operate="add" FunctionType="Add" />
    <item text="移除组织" href="Groupremove.aspx" operate="remove" FunctionType="Delete"/>
    </group>
    <role>
    <item text="搜索姓名" href="Rolelist.aspx" FunctionType="View"/>
    <item text="添加姓名" href="Roleadd.aspx" operate="add" FunctionType="Add"/>
    <item text="移除姓名" href="Roleremove.aspx" operate="remove" FunctionType="Delete"/>
    </role>
    <user>
    <item text="搜索" href="userlist.aspx" FunctionType="View"/>
    <item text="添加" href="useradd.aspx" operate="add" FunctionType="Add"/>
    <item text="导入" href="userimport.aspx" FunctionType="View"/>
    <item text="重置密码" href="usersetpassword.aspx" operate="remove" FunctionType="Modify"/>
    <item text="移除" href="userremove.aspx" operate="remove" FunctionType="Delete"/>
    </user>
    <application>
    <item text="搜索系统" href="applicationlist.aspx" FunctionType="View"/>
    <item text="添加系统" href="applicationadd.aspx" operate="add" FunctionType="Add"/>
    <item text="移除系统" href="applicationremove.aspx" operate="remove" FunctionType="Delete"/>
    </application>
    <manage>
    <item text="基本信息授权" href="Authorization.aspx" operate="others" FunctionType="View"/>
    </manage>
    </leftMenu>

          

          C#代码的增、删、改、查如下,基本都是实践出来,实在是不知道的再网上查查
      

            #region 读取XML文件内容
    private void ReadXmlFile()
    {
    string strXML = string.Empty;

    XmlDocument xmldoc = new XmlDocument();

    //载入xml文件
    xmldoc.Load(@"E:\menu.xml");

    //根据根节点名称读取xml文件内容
    XmlNodeList xmlnodelist = xmldoc.SelectSingleNode("leftMenu").ChildNodes;

    #region 用于显示节点内容,可以删除
    List<XmlNode> list = new List<XmlNode>();


    for (int i = 0; i < xmlnodelist.Count; i++)
    {
    list.Add(xmlnodelist.Item(i));
    }
    #endregion

    //获取xml文件的XmlElement节点
    List<XmlElement> listElement = new List<XmlElement>();

    foreach (XmlNode item in xmlnodelist)
    {
    XmlNodeList listtemp = item.ChildNodes;

    //此处稍微特别,需要读取两次节点才能读取到XmlElement节点
    for (int i = 0; i < listtemp.Count; i++)
    {
    listElement.Add((XmlElement)listtemp[i]);
    }
    }

    //显示读取的内容到页面
    foreach (var value in listElement)
    {
    strXML = strXML + value.GetAttribute("text") + "<br/>";

    }

    Response.Write(strXML);

    }
    #endregion

    #region 新增xml文件节点内容
    private void AddXmlFile()
    {
    XmlDocument xmldoc = new XmlDocument();

    //载入xml文件
    xmldoc.Load(@"E:\menu.xml");

    //根据根节点名称读取xml文件内容
    XmlNodeList xmlnodelist = xmldoc.SelectSingleNode("leftMenu").ChildNodes;


    string strXml = @"<item text='星期一' href='userlist.aspx' FunctionType='View' />
    <item text='星期二' href='useradd.aspx' operate='add' FunctionType='Add' />
    <item text='星期三' href='userimport.aspx' FunctionType='View' />
    <item text='星期四' href='usersetpassword.aspx' operate='remove' FunctionType='Modify' />
    <item text='星期五' href='userremove.aspx' operate='remove' FunctionType='Delete' />
    ";




    XmlNode xn = xmldoc.SelectSingleNode("leftMenu");
    //if (element.Equals(""))
    //{
    //if (!attribute.Equals(""))
    //{


    ////设置节点属性用的
    //XmlElement xe = (XmlElement)xn;
    //xe.SetAttribute("Test", strXml);


    //}
    //}
    //else
    //{

    //创建节点
    XmlElement xe = xmldoc.CreateElement("Practice");


    //if (attribute.Equals(""))
    // xe.InnerText = value;
    //else


    //设置节点xml内容
    xe.InnerXml = strXml;
    xn.AppendChild(xe);



    //}

    xmldoc.Save(@"E:\menu.xml");






    }
    #endregion

    #region 修改xml文件节点内容
    private void ModifyXmlFile()
    {
    XmlDocument xmldoc = new XmlDocument();

    //载入xml文件
    xmldoc.Load(@"E:\menu.xml");

    string strXml = "测试最终修改属性";

    //根据根节点名称读取xml文件内容
    XmlNodeList xmlnodelist = xmldoc.SelectSingleNode("leftMenu").ChildNodes;

    XmlNode xn = xmldoc.SelectSingleNode("leftMenu");
    //设置节点属性用的
    XmlElement xe = (XmlElement)xn;
    xe.SetAttribute("TestAddAttribute", strXml);

    xmldoc.Save(@"E:\menu.xml");
    }
    #endregion

    #region 删除xml文件节点内容
    private void DeleteXmlFile()
    {
    XmlDocument xmldoc = new XmlDocument();

    //载入xml文件
    xmldoc.Load(@"E:\menu.xml");

    //根据根节点名称读取xml文件内容
    XmlNodeList xmlnodelist = xmldoc.SelectSingleNode("leftMenu").ChildNodes;


    //XmlNode XmlNodeRemove= xmldoc.SelectSingleNode("leftMenu").RemoveChild(xmlnodelist[xmlnodelist.Count - 1]);




    //xmldoc.Save(@"E:\menu.xml");



    XmlElement XmlElementRemove =(XmlElement) xmldoc.GetElementsByTagName("manage")[0];


    XmlNode XmlNodeRemoveTwo = xmldoc.SelectSingleNode("leftMenu").RemoveChild((XmlNode)XmlElementRemove);

    xmldoc.Save(@"E:\menu.xml");
    }
    #endregion

      操作之后的结果如下:

        

    <?xml version="1.0" encoding="utf-8"?>
    <leftMenu TestAddAttribute="测试最终修改属性">
    <directory>
    <item text="搜索地方" href="directoryList.aspx" FunctionType="View" />
    <item text="添加地方" href="directoryAdd.aspx" operate="add" FunctionType="Add" />
    <item text="移除地方" href="directoryRemove.aspx" operate="remove" FunctionType="Delete" />
    </directory>
    <group>
    <item text="搜索组织" href="Grouplist.aspx" FunctionType="View" />
    <item text="添加组织" href="Groupadd.aspx" operate="add" FunctionType="Add" />
    <item text="移除组织" href="Groupremove.aspx" operate="remove" FunctionType="Delete" />
    </group>
    <role>
    <item text="搜索姓名" href="Rolelist.aspx" FunctionType="View" />
    <item text="添加姓名" href="Roleadd.aspx" operate="add" FunctionType="Add" />
    <item text="移除姓名" href="Roleremove.aspx" operate="remove" FunctionType="Delete" />
    </role>
    <user>
    <item text="搜索" href="userlist.aspx" FunctionType="View" />
    <item text="添加" href="useradd.aspx" operate="add" FunctionType="Add" />
    <item text="导入" href="userimport.aspx" FunctionType="View" />
    <item text="重置密码" href="usersetpassword.aspx" operate="remove" FunctionType="Modify" />
    <item text="移除" href="userremove.aspx" operate="remove" FunctionType="Delete" />
    </user>
    <application>
    <item text="搜索系统" href="applicationlist.aspx" FunctionType="View" />
    <item text="添加系统" href="applicationadd.aspx" operate="add" FunctionType="Add" />
    <item text="移除系统" href="applicationremove.aspx" operate="remove" FunctionType="Delete" />
    </application>
    <Practice>
    <item text="星期一" href="userlist.aspx" FunctionType="View" />
    <item text="星期二" href="useradd.aspx" operate="add" FunctionType="Add" />
    <item text="星期三" href="userimport.aspx" FunctionType="View" />
    <item text="星期四" href="usersetpassword.aspx" operate="remove" FunctionType="Modify" />
    <item text="星期五" href="userremove.aspx" operate="remove" FunctionType="Delete" />
    </Practice>
    </leftMenu>

      总结:我也知道虽然实现了一些基本的东西,但是很不完善而且代码简直就是一团糟,有一点收获就是简单的知道一点点东西,想法是从这个点出发,一点破面。

        与此同时也有点纠结一些事儿,11年7月左右转正,当时是什么都不会什么都不知道,js、jquery、css都是没有学没有用过,而且由于自己学东西到实践运用需要时间比较长所以感觉自己总是很害怕,怕自己提升的不够。毕业之后就一步一步的积累,但是没有实践就像无厘头一样到处乱穿,过了也许就忘了,同时在这一段时间里面是看的书越多,读的博客越多就越感觉到越来越多的东西不懂,更别说运用。

        到现在项目就做了那么一两个,而且待遇也不是很好,想换个环境,虽然知道比起刚入职时的酱油状态好一些又好不了多少,但是又怕出去了混不走啊,很迷茫。既不想这样子过下去又怕出去了...。在此,请各位给我一些参考意见。3Q

              

    ※如果你觉得这篇文章不错,请点击推荐。如果你觉得我写的对你有用请关注我。
    作者:Max蚊子
    网站:feiger.cn         飞鸽博客,关注互联网、站长圈的程序员博客!
                 
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Python修改文件内容
    Python实现用户注册到文件
    Postman接口测试
    Linux下安装LoadRunner LoadGenerator
    Loadrunner参数化避免重复数据
    Ta-Lib用法介绍 !
    迭代器 生成器
    深入理解python多进程编程
    python多进程
    python多线程
  • 原文地址:https://www.cnblogs.com/kim01/p/2400754.html
Copyright © 2020-2023  润新知