XML帮助类

 public class XMLHelper
        {
            public string Path { get; set; }//路径
            //带有一个路径的参数构造方法                               
            public XMLHelper(string path)
            {
                this.Path = path;
            }
            //创建xml并创建根节点
            public void AddXml(string rootName)
            {
                XmlDocument doc = new XmlDocument();
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
                doc.AppendChild(dec);
                XmlNode root = doc.CreateElement(rootName);
                doc.AppendChild(root);
                doc.Save(this.Path);
            }
            //新增节点
            public void AddNode(string nodeName, string value)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(this.Path);
                XmlNode root = doc.DocumentElement;
                XmlNode node = doc.CreateElement(nodeName);
                node.InnerText = value;
                root.AppendChild(node)
                doc.Save(this.Path);
            }
            //删除节点方法  
            public void DelNode(string nodeName, string xpath)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(this.Path);
                XmlNode xmlNode = doc.SelectSingleNode(xpath);
                foreach (XmlNode node in xmlNode.ChildNodes)
                {
                    if (node.Name.Equals(nodeName))
                    {
                        xmlNode.RemoveChild(node);
                        break;
                    }
                }
                doc.Save(this.Path);
            }
            //保存文件
            public void SaveXml(XmlDocument doc)
            {
                doc.Save(this.Path);
            }
            //创建根节点对象
            public void AddRoot(string rootName)
            {
                //不知道是什么意思
            }
            //获取指定XPath表达式节点的值
            public string GetNodeValue(string xpath)
            {
                string str = "";
                XmlDocument doc = new XmlDocument();
                doc.Load(this.Path);
                XmlNode xmlNode = doc.SelectSingleNode(xpath);
                foreach (XmlNode node in xmlNode.ChildNodes)//获取所有的子节点
                {
                    str += node.InnerText + "-";
                }
              return str;
            }
            //获取指定XPath表达式节点的属性值方法
            public string GetNodeNameValue(string xpath)
            {
                string str = "";
                XmlDocument doc = new XmlDocument();
                doc.Load(this.Path);
                XmlNode xmlNode = doc.SelectSingleNode(xpath);
                foreach (XmlNode node in xmlNode.ChildNodes)
                {
                    str += node.Attributes["type"].Value + "-";
                }
               return str;
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值