xml操作

using System;
using System.Xml;
using System.Collections.Generic;
using System.Text;
namespace janyo.io
{
   public class XmlUtility
    {
        private static string xmlfilename="";
    
        public static string XmlFileName
        {
            set
            {
                xmlfilename=value;
            }
        }
        private void loadXmlDoc()
        {
            //XmlDocument xdoc = new XmlDocument();
            //xdoc.Load(xmlfilename);
          
        }
       private static string getFileName()
       {
           return "";
       }
        /// <summary>
        /// 读取节点的值
        /// </summary>
        /// <param name="node">节点名称</param>
        /// <returns></returns>
        public static string getValueByNode(string node)
        {
           string temp ="";
             XmlTextReader xrd =new XmlTextReader(xmlfilename);
           
            while (xrd.Read())
            {
                if (xrd.NodeType==XmlNodeType.Element)
                {
                    if (xrd.Name==node)
                    {
                     
                   temp=xrd.ReadElementContentAsString();
                   return temp;
                      
                    }
                 }
            }
            xrd.Close();
            return "";
        }
       /// <summary>
       /// 查找某个所有匹配条件的节点值
       /// </summary>
        /// <param name="xpath">节点xpath表达式;/siteconfig/item[@id=var]</param>
       /// <returns></returns>
        public static System.Collections.ArrayList getItems(string xpath)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(xmlfilename);
            XmlNodeList xnodelist = xmldoc.SelectNodes(xpath);
            System.Collections.ArrayList arr = new System.Collections.ArrayList();
           if (xnodelist.Count==0)
         {
           return null;
         }
            foreach (XmlNode xnod in xnodelist)
            {
                arr.Add(xnod.InnerText);
            }
            xmldoc = null;
            
            return arr;
        }
        /// <summary>
        /// 读取某个节点的属性值
        /// </summary>
        /// <param name="node">节点名称</param>
        /// <param name="attrs">属性,可以读写该节点多个属性</param>
        /// <returns></returns>
       public static System.Collections.ArrayList getValueByNode(string node,params string[] attrs)
        {
            
             XmlReader xrd = XmlReader.Create(xmlfilename);
            while (xrd.Read())
            {
                if (xrd.NodeType == XmlNodeType.Element)
                {
                   
                    if(xrd.Name==node)
                    {
                        //string[] temp=null;
                        System.Collections.ArrayList arlist = new System.Collections.ArrayList();
                       // int i =0;
                        foreach (string attr in attrs)
                        {
                           arlist.Add(xrd.GetAttribute(attr));
                           // i++;
                        }
                        return arlist;
                    }
                }
            }
            xrd.Close();
            return null;
        }
        /// <summary>
        /// 删除某个节点
        /// </summary>
        /// <param name="xpath">节点xpath表达式;/siteconfig/item[@id=var]</param>
        public static void deletNode(string xpath)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(xmlfilename);
           XmlNode xnod=xmldoc.SelectSingleNode(xpath);
           xnod.ParentNode.RemoveChild(xnod);
            xmldoc.Save(xmlfilename);
           
        }
        /// <summary>
        /// 更新某个节点的值
        /// </summary>
        /// <param name="xpath">节点xpath表达式;/siteconfig/item[@id=var]</param>
        /// <param name="value">更新值<node></node></param>
        public static void updateNode(string xpath, string value)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(xmlfilename);
            XmlNode xnod = xmldoc.SelectSingleNode(xpath);
            xnod.InnerText = value;
            xmldoc.Save(xmlfilename);
            xmldoc = null;
        }
       /// <summary>
        /// 批量更新节点屬性的值
        /// </summary>
        /// <param name="xpath">节点xpath表达式;/siteconfig/item[@id=var]</param>
        /// <param name="attrs">屬性名稱值對<node></node></param>
        public static void updateNode(string xpath, string[] attrs)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(xmlfilename);
            XmlNodeList xnodlist = xmldoc.SelectNodes(xpath);
            foreach (XmlNode xnod in xnodlist)
            {
                xnod.Attributes[attrs[0]].Value=attrs[1];
            }
          //  xnod.InnerText = value;
            xmldoc.Save(xmlfilename);
            xmldoc = null;
        }
        /// <summary>
        /// 在指定节点插入子节点
        /// </summary>
        /// <param name="xpath">节点xpath表达式;/siteconfig/item[@id=var]</param>
        /// <param name="value">节点值<node></node></param>
        /// <param name="cNode">待插入的节点</param>
        public static void insertNode(string xpath, string cNode,string value,params string[] attrs)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(xmlfilename);
            XmlNode xnod = xmldoc.SelectSingleNode(xpath);
            XmlElement xn = xmldoc.CreateElement(cNode);
            int attrsLength = attrs.GetUpperBound(0)+1;
            if (attrsLength%2!=0)
            {
                throw new Exception("屬性名稱值對不相等");
            }
            for (int i = 0; i <attrs.GetUpperBound(0)+1; i++)
            {
                xn.SetAttribute(attrs[i], attrs[i+1]);
                i++;
            }            
            xnod.AppendChild(xn);
            xnod.LastChild.InnerText=value;
            xmldoc.Save(xmlfilename);
            xmldoc = null;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值