XMLHelper类

 

 

创建XML文档

            //这是XML文档根节点名
            string rootNodeName = "books";

            //这是XML文档物理文件名(包含物理路径)
            string xmlFileName = Application.StartupPath + @"/book.xml";

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


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

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

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



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

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

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

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

            string xmlFileName = Application.StartupPath + @"/book.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());

删除XML文档中的子节点:

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

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

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

            string xmlFileName = Application.StartupPath + @"/book.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());

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

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值