XML 文件解析帮助类

XML文件解析帮助类 

   class ReadXMLHelper
    {
        #region 读取XML配置文件
        /// <summary>
        /// 读取XML配置文件  失败返回null
        /// </summary>
        /// <param name="filePath">xml文件路径</param>
        /// <returns>xml文档对象</returns>
        public static XmlDocument readXml(string filePath)
        {
            try
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(filePath);
                return xml;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }
        #endregion

        #region 写入XML配置文件
        /// <summary>
        ///  写入XML配置文件 成功返回True 失败返回false
        /// </summary>
        /// <param name="xml">xml对象</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static Boolean writeXml(XmlDocument xml, string filePath)
        {
            try
            {
                //把读取获得的XML写入到filePath
                xml.Save(filePath);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }
        #endregion

        #region 匹配 XPath 表达式的第一个 XmlNode
        /// <summary>
        /// 匹配 XPath 表达式的第一个 XmlNode
        /// </summary>
        /// <param name="xml">xml文档对象</param>
        /// <param name="xPath">xPath为层级父节点</param>
        /// <returns>xml节点对象失败返回Null</returns>
        public static XmlNode getXmlNode(XmlDocument xml, string xPath)
        {
            //选择匹配 XPath 表达式的第一个 XmlNode
            XmlNode xmlNode = xml.SelectSingleNode(xPath);
            //读取节点数据
            if (xmlNode != null)
            {
                return xmlNode;
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region 获取节点text
        /// <summary>
        /// 获取节点text
        /// </summary>
        /// <param name="xml获取节点textNode">节点对象</param>
        /// <returns>返回null则失败,返回""则代表节点内容为空,成功返回节点text</returns>
        public static string getNodeText(XmlNode xmlNode)
        {
            //读取节点数据
            if (xmlNode != null)
            {
                string nodeText = xmlNode.InnerText;
                if (nodeText != null)
                {
                    return nodeText;
                }
                else
                {
                    return "";
                }
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region 根据xPath获取节点个数
        /// <summary>
        /// 根据xPath获取节点个数
        /// </summary>
        /// <param name="xml">xml文档对象</param>
        /// <param name="xPath">xPath为层级父节点</param>
        /// <returns>返回符合xPath的节点数,没有则返回0</returns>
        public static int getCountByXpath(XmlDocument xml, string xPath)
        {
            //读取节点list
            XmlNodeList nodelist = xml.SelectNodes(xPath);
            if (nodelist != null)
            {
                return nodelist.Count;
            }
            else
            {
                return 0;
            }
        }
        #endregion

        #region 根据xPath获取节点列表
        /// <summary>
        ///  根据xPath获取节点列表
        /// </summary>
        /// <param name="xml">xml文档对象</param>
        /// <param name="xPath">xPath 为层级父节点</param>
        /// <returns>返回符合xPath的节点列表,失败返回null</returns>
        public static XmlNodeList getNodeListByXpath(XmlDocument xml, string xPath)
        {
            //读取节点list
            XmlNodeList nodelist = xml.SelectNodes(xPath);
            if (nodelist != null)
            {
                return nodelist;
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region XML转DataTable
        public DataTable _GetPOAddTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("AA", typeof(string)));
            dt.Columns.Add(new DataColumn("BB", typeof(string)));
            dt.Columns.Add(new DataColumn("CC", typeof(string)));
            return dt;
        }
        /// <summary>
        /// 把多个同级节点下的子节点AA、BB、CC读出放入Table的AA、BB、CC列
        /// </summary>
        /// <param name="filePath">xml文件路径</param>
        /// <returns></returns>
        public DataTable XMLParse(string filePath)
        {
            DataTable addDt = _GetPOAddTable();
            FileStream fr = null;

            try
            {
                XmlDocument doc;
                doc = new XmlDocument();
                XmlNodeList poRoot;

                fr = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, 8, false);
                byte[] bt = new byte[fr.Length];
                fr.Read(bt, 0, bt.Length);
                fr.Close();
                string sFileContent = Encoding.UTF8.GetString(bt);
                DataRow addRow;

                doc.LoadXml(sFileContent);
                string MyInfo = sFileContent;
                string[] arrylist = MyInfo.Split('\t');

                //result = ReadIn(arrylist, File_Name, getFile_Time, file_createtime);
                poRoot = doc.SelectSingleNode("A").ChildNodes;
                foreach (XmlNode tmpNode in poRoot)
                {
                    addRow = addDt.NewRow();
                    addRow["AA"] = tmpNode.Attributes["A"].Value;
                    addRow["BB"] = tmpNode.SelectSingleNode("B").SelectSingleNode("BB").InnerText;
                    addRow["CC"] = tmpNode.Attributes["CC"].Value;
                    addDt.Rows.Add(addRow);
                }

            }
            catch (Exception ex)
            {

            }
            finally
            {
                fr.Close();
            }
            return addDt;
        }
        #endregion

    } 
posted @ 2021-05-04 14:53  码农阿亮  阅读(106)  评论(0编辑  收藏  举报