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 }
本文来自博客园,作者:码农阿亮,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/14729511.html
技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
开源库地址,欢迎点亮:
GitHub:https://github.com/ITMingliang
Gitee: https://gitee.com/mingliang_it
GitLab: https://gitlab.com/ITMingliang
建群声明: 本着技术在于分享,方便大家交流学习的初心,特此建立【编程内功修炼交流群】,为大家答疑解惑。热烈欢迎各位爱交流学习的程序员进群,也希望进群的大佬能不吝分享自己遇到的技术问题和学习心得!进群方式:扫码关注公众号,后台回复【进群】。