• xml操作


    转:http://www.cnblogs.com/chnboy/archive/2009/04/02/1427652.html

    经过这些日子以来,我根据上篇随笔中读写程序配置文件节点的方法来扩展了其功能,并写了这个XML文档的操作管理帮助类XMLHelper出来, 这个XMLHelper类中包括了XML文档的创建,文档节点和属性的读取,添加,修改,删除的方法功能的实现,有兴趣的朋友,可以进来看看,所有代码都 在WebForm和WinForm中调试通过.

    参考我的上篇随笔:关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作

    这是下面要操作的XML文档:


    <?xml version="1.0" encoding="utf-8"?>
    <books>
      <book id="1" ISDN="1001001001">
        <name>我的世界我的梦</name>
        <author>姚明</author>
        <date>2008-09-23</date>
      </book>
      <book id="2" ISDN="2002000230032">
        <name>围城</name>
        <author>钱钟书</author>
        <date>2008-09-23</date>
      </book>
      <book id="3" />
    </books>

    以下是XMLHelper文档操作帮助类代码:

     XMLHelper下载:http://i.cnblogs.com/Files.aspx

    1.创建XML文档:

    复制代码
         //这是XML文档根节点名
                string rootNodeName = "books";
                
                //这是XML文档物理文件名(包含物理路径)
                string xmlFileName = Application.StartupPath + @"ook.xml";

                XMLHelper.CreateXmlDocument(xmlFileName, rootNodeName, "1.0""utf-8"null);
                MessageBox.Show("XML文档创建成功:" + xmlFileName);
    复制代码

    2.向XML文档中添加一个新节点:

    复制代码
                string xmlFileName = Application.StartupPath + @"ook.xml";
                string xpath = "/books";  //这是新节点的父节点路径
                string nodename = "book"; //这是新节点名称,在父节点下新增
                string nodetext = "这是新节点中的文本值";

                bool isSuccess = XMLHelper.CreateOrUpdateXmlNodeByXPath(xmlFileName, xpath, nodename, nodetext);
                MessageBox.Show("XML节点添加或更新成功:" + isSuccess.ToString());
    复制代码

    3.向XML文档中的子节点中新增或修改(如果存在则修改)一个子节点,比如name,author,date节点等:

    复制代码
                string xmlFileName = Application.StartupPath + @"ook.xml";
                string xpath = "/books/book";  //这是新子节点的父节点路径
                string nodename = "name"; //这是新子节点名称,在父节点下新增
                string nodetext = "我的世界我的梦";

                bool isSuccess = XMLHelper.CreateOrUpdateXmlNodeByXPath(xmlFileName, xpath, nodename, nodetext);
                MessageBox.Show("XML节点添加或更新成功:" + isSuccess.ToString());
    复制代码

    4. 向XML文档中的子节点中新增或修改(如果存在则修改)一个子节点属性,比如id,ISDN属性等:

    复制代码
                string xmlFileName = Application.StartupPath + @"ook.xml";
                string xpath = "/books/book"//要新增属性的节点
                string attributeName = "id"; //新属性名称,ISDN号也是这么新增的
                string attributeValue = "1"; //新属性值

                bool isSuccess = XMLHelper.CreateOrUpdateXmlAttributeByXPath(xmlFileName, xpath, attributeName, attributeValue);
                MessageBox.Show("XML属性添加或更新成功:" + isSuccess.ToString());
    复制代码

    5. 删除XML文档中的子节点:

                string xmlFileName = Application.StartupPath + @"ook.xml";
                string xpath = "/books/book[@id='1']"//要删除的id为1的book子节点

                bool isSuccess = XMLHelper.DeleteXmlNodeByXPath(xmlFileName, xpath);
                MessageBox.Show("XML节点删除成功:" + isSuccess.ToString());

    6. 删除XML文档中子节点的属性:

    复制代码
                string xmlFileName = Application.StartupPath + @"ook.xml";
                //删除id为2的book子节点中的ISDN属性
                string xpath = "/books/book[@id='2']";
                string attributeName = "ISDN";

                bool isSuccess = XMLHelper.DeleteXmlAttributeByXPath(xmlFileName, xpath,attributeName);
                MessageBox.Show("XML属性删除成功:" + isSuccess.ToString());
    复制代码

    7.读取XML文档中的所有子节点:

    复制代码
                string xmlFileName = Application.StartupPath + @"ook.xml";
    //要读的id为1的book子节点
                string xpath = "/books/book[@id='1']";

                XmlNodeList nodeList = XMLHelper.GetXmlNodeListByXpath(xmlFileName, xpath);
                string strAllNode = "";
                //遍历节点中所有的子节点
                foreach (XmlNode node in nodeList)
                {
                    strAllNode += "  name:" + node.Name + " InnerText:" + node.InnerText;
                }

                MessageBox.Show("XML节点中所有子节点有:" + strAllNode);
    复制代码

    8.其它的方法我就不一一的例举了,各位自己动手去尝试便知,关键的地方就是那个xpath的参数设置了,

    这个是XML文档中xpath语法,大家去网上一查便明白,好了,我要休息去,明白偶还有很多的工作做...

  • 相关阅读:
    【CF446D】DZY Loves Games 高斯消元+矩阵乘法
    【CF542D】Superhero's Job 暴力
    【CF660E】Different Subsets For All Tuples 结论题
    【CF666C】Codeword 结论题+暴力
    【CF666E】Forensic Examination 广义后缀自动机+倍增+线段树合并
    【CF461E】Appleman and a Game 倍增floyd
    【CF471E】MUH and Lots and Lots of Segments 扫描线+并查集+线段树+set
    【CF480D】Parcels DP
    【CF497E】Subsequences Return 矩阵乘法
    SSAS 项目部署失败的问题
  • 原文地址:https://www.cnblogs.com/sdya/p/4274823.html
Copyright © 2020-2023  润新知