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;
        }
    }
}

 

你可能感兴趣的:(xml)