当前位置:编程学习 > XML/UML >>

c# xml操作类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.IO;
 
namespace 人事管理系统.Helper
{
    public  class xmlHelper
    {
        #region 字段
        /// <summary>
        /// xml文件物理路径
        /// </summary>
        private  string _FilePath = string.Empty;
        /// <summary>
        /// xml文档
        /// </summary>
        private XmlDocument _xml;
        /// <summary>
        /// xml文档根节点
        /// </summary>
        private XmlElement _element;
        #endregion
 
        public xmlHelper()
        {
            //
        }
 
        /// <summary>
        /// 给xml文档路径赋值
        /// </summary>
        /// <param name="xmlFilePath"></param>
        public xmlHelper(string xmlFilePath)
        {
            _FilePath = xmlFilePath;
        }
 
        /// <summary>
        /// 获取指定路径节点
        /// </summary>
        /// <param name="xPath"></param>
        /// <returns></returns>
        public static XmlNode GetXmlNode(string xmlFileName, string xPath)
        {
            XmlDocument xmldocument = new XmlDocument();
            //加载xml文档
            xmldocument.Load(xmlFileName);
            try
            {
                XmlNode xmlnode = xmldocument.SelectSingleNode(xPath);
                return xmlnode;
            }
            catch
            {
                return null;
            }
        }
 
        /// <summary>
        /// 获取指定路径节点下孩子节点列表
        /// </summary>
        /// <param name="xmlFileName"></param>
        /// <param name="xPath"></param>
        /// <returns></returns>
        public static XmlNodeList GetXmlNodeList(string xmlFileName, string xPath)
        {
            XmlDocument xmldocument = new XmlDocument();
            //加载xml文档
            xmldocument.Load(xmlFileName);
            try
            {
                XmlNodeList xmlnodelist = xmldocument.SelectNodes(xPath);
                return xmlnodelist;
            }
            catch
            {
                return null;
            }
        }
 
        /// <summary>
        /// 获取指定路径节点的属性与指定属性名匹配
        /// </summary>
        /// <param name="xmlFileName"></param>
        /// <param name="xPath">要匹配的XPath表达式(例如:"//节点名//子节点名</param>
        /// <param name="attributeName">指定的属性名称</param>
        /// <returns></returns>
        public static XmlAttribute GetXmlAttribute(string xmlFileName, string xPath,string attributeName)
        {
            XmlAttribute xmlattribute=null;
            XmlDocument xmldocument = new XmlDocument();
            xmldocument.Load(xmlFileName);
            try
            {
                XmlNode xmlnode = xmldocument.SelectSingleNode(xPath);
                if (xmlnode != null)
                {
                    if (xmlnode.Attributes.Count > 0)
                    {
                        xmlattribute = xmlnode.Attributes[attributeName];
                    }
                }
            }
            catch (Exception err)
            {
                throw err;
            }
            return xmlattribute;
        }
 
        /// <summary>
        /// 获取指定节点的属性集合
        /// </summary>
        /// <param name="xmlFileName"></param>
        /// <param name="xPath"></param>
        /// <returns></returns>
        public static XmlAttributeCollection GetNodeAttributes(string xmlFileName, string xPath)
        {
         
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,