基於C#完成XML文件讀取對象類。本站提示廣大學習愛好者:(基於C#完成XML文件讀取對象類)文章只能為提供參考,不一定能成為您想要的結果。以下是基於C#完成XML文件讀取對象類正文
本文實例講述了基於C#完成XML文件讀取對象類。分享給年夜家供年夜家參考。詳細以下:
這是我客歲寫的一個XML文件讀取對象類,如今做了一些調劑
根本能知足普通的XML文件讀取需求。
更新:
2013年6月26日19:37 Fix了幾個Bug,為一切辦法添加單位測試,並運轉經由過程;同時調劑了幾個辦法的地位。
/// <summary> /// Author: jiangxiaoqiang /// </summary> public class XmlReader { //========================================================= // #region 獲得XmlDocument對象 /// <summary> /// 依據XML文件內容獲得XmlDocument對象 /// </summary> /// <param name="xmlFileContent"></param> /// <returns></returns> public static XmlDocument GetXmlDocByXmlContent(string xmlFileContent) { if (string.IsNullOrEmpty(xmlFileContent)) { return null; } var xDoc = new XmlDocument(); try { xDoc.LoadXml(xmlFileContent); } catch { xDoc = null; } return xDoc; } /// <summary> /// 依據XML文件途徑獲得XmlDocument對象 /// </summary> /// <param name="xmlFilePath"></param> /// <returns></returns> public static XmlDocument GetXmlDocByFilePath(string xmlFilePath) { if (string.IsNullOrEmpty(xmlFilePath) || !File.Exists(xmlFilePath)) { return null; } var xDoc = new XmlDocument(); try { xDoc.Load(xmlFilePath); } catch { throw new Exception(string.Format("請確認該XML文件格局准確,途徑為:{0}", xmlFilePath)); } return xDoc; } #endregion //========================================================= // //========================================================= // #region 獲得XML節點(或節點列表) /// <summary> /// 獲得父節點下指定節點稱號的第一個子節點 /// </summary> /// <param name="parentXmlNode"></param> /// <param name="childNodeName"></param> /// <returns></returns> public static XmlNode GetFirstChildNodeByName(XmlNode parentXmlNode, string childNodeName) { var childXmlNodes = GetChildNodesByName(parentXmlNode, childNodeName); if (childXmlNodes != null && childXmlNodes.Count > 0) { return childXmlNodes[0]; } return null; } /// <summary> /// 獲得父節點下指定節點稱號的子節點列表 /// </summary> /// <param name="parentXmlNode">父節點</param> /// <param name="nodeName">節點稱號</param> /// <returns></returns> public static XmlNodeList GetChildNodesByName(XmlNode parentXmlNode, string nodeName) { if (parentXmlNode == null || string.IsNullOrEmpty(nodeName)) { return null; } return GetChildNodesByXPathExpr(parentXmlNode, string.Format(".//{0}", nodeName)); } /// <summary> /// 獲得父節點下知足xpathExpr表達式的XML子節點列表 /// </summary> /// <param name="parentXmlNode">父節點</param> /// <param name="xpathExpr"></param> /// <returns></returns> public static XmlNodeList GetChildNodesByXPathExpr(XmlNode parentXmlNode, string xpathExpr) { if (parentXmlNode == null || string.IsNullOrEmpty(xpathExpr)) { return null; } return parentXmlNode.SelectNodes(xpathExpr); } /// <summary> /// 獲得父節點下的第一個子節點 /// </summary> /// <param name="parentXmlNode"></param> /// <returns></returns> public static XmlNode GetFirstChildNode(XmlNode parentXmlNode) { var childXmlNodes = GetChildNodes(parentXmlNode); if (childXmlNodes != null && childXmlNodes.Count > 0) { return childXmlNodes[0]; } return null; } /// <summary> /// 獲得父節點的子節點列表 /// </summary> /// <param name="parentXmlNode">父節點</param> /// <returns></returns> public static XmlNodeList GetChildNodes(XmlNode parentXmlNode) { return parentXmlNode == null ? null : parentXmlNode.ChildNodes; } #endregion //========================================================= // //========================================================= // #region 讀取節點屬性值 /// <summary> /// 讀取某個XML節點的屬性值(依據屬性名) /// </summary> /// <param name="xmlNode"></param> /// <param name="attrName"></param> /// <returns></returns> public static string ReadAttrValue(XmlNode xmlNode, string attrName) { var xmlElement = xmlNode as XmlElement; return xmlElement == null ? null : xmlElement.GetAttribute(attrName); } /// <summary> /// 讀取父節點下指定節點名和屬性名的第一個子節點的屬性值 /// </summary> /// <param name="parentXmlNode">XML父節點</param> /// <param name="childNodeName">節點稱號</param> /// <param name="attrName">屬性名</param> /// <returns></returns> public static string ReadFirstAttrValue(XmlNode parentXmlNode, string childNodeName, string attrName) { var attrVals = ReadAttrValues(parentXmlNode, childNodeName, attrName); return (attrVals == null || attrVals.Length == 0) ? null : attrVals[0]; } /// <summary> /// 讀取父節點下指定節點名和屬性名的一切子節點的該屬性值的數組 /// </summary> /// <param name="parentXmlNode">XML文檔</param> /// <param name="childNodeName">節點稱號</param> /// <param name="attrName">屬性名</param> /// <returns></returns> public static string[] ReadAttrValues(XmlNode parentXmlNode, string childNodeName, string attrName) { if (parentXmlNode == null || string.IsNullOrEmpty(childNodeName) || string.IsNullOrEmpty(attrName)) { return null; } var xpathExpr = string.Format("//{0}[@{1}]", childNodeName, attrName); var nodes = GetChildNodesByXPathExpr(parentXmlNode, xpathExpr); if (nodes != null && nodes.Count > 0) { var nodeCount = nodes.Count; var attrVals = new string[nodeCount]; for (var i = 0; i < nodeCount; i++) { attrVals[i] = ((XmlElement)nodes[i]).GetAttribute(attrName); } return attrVals; } return null; } #endregion //========================================================= // //========================================================= // #region 讀取父節點下的子節點的文本內容 /// <summary> /// 讀取父節點下指定節點名的第一個子節點的文本 /// </summary> /// <param name="parentXmlNode"></param> /// <param name="childNodeName"></param> /// <returns></returns> public static string ReadFirstChildNodeTextByName(XmlNode parentXmlNode, string childNodeName) { var childNodeTexts = ReadChildNodeTextsByName(parentXmlNode, childNodeName); if (childNodeTexts != null && childNodeTexts.Length > 0) { return childNodeTexts[0]; } return null; } /// <summary> /// 讀取父節點下指定節點名的一切子節點的文本數組 /// </summary> /// <param name="parentXmlNode"></param> /// <param name="childNodeName"></param> /// <returns></returns> public static string[] ReadChildNodeTextsByName(XmlNode parentXmlNode, string childNodeName) { if (parentXmlNode == null || string.IsNullOrEmpty(childNodeName)) { return null; } var xpathExpr = string.Format(".//{0}", childNodeName); var childNodes = GetChildNodesByXPathExpr(parentXmlNode, xpathExpr); if (childNodes != null && childNodes.Count > 0) { var nodeCount = childNodes.Count; var nodeTexts = new string[nodeCount]; for (var i = 0; i < nodeCount; i++) { nodeTexts[i] = childNodes[i].InnerText; } return nodeTexts; } return null; } /// <summary> /// 讀取父節點下的第一個子節點的文本 /// </summary> /// <param name="parentXmlNode"></param> /// <returns></returns> public static string ReadFirstChildNodeText(XmlNode parentXmlNode) { var childNodeTexts = ReadChildNodeTexts(parentXmlNode); if (childNodeTexts != null && childNodeTexts.Length > 0) { return childNodeTexts[0]; } return null; } /// <summary> /// 讀取父節點下的一切子節點的文本數組 /// </summary> /// <param name="parentXmlNode"></param> /// <returns></returns> public static string[] ReadChildNodeTexts(XmlNode parentXmlNode) { if (parentXmlNode == null) { return null; } var childNodes = GetChildNodes(parentXmlNode); if (childNodes != null && childNodes.Count > 0) { var nodeCount = childNodes.Count; var nodeTexts = new string[nodeCount]; for (var i = 0; i < nodeCount; i++) { nodeTexts[i] = childNodes[i].InnerText; } return nodeTexts; } return null; } /// <summary> /// 讀取XML節點文本 /// </summary> /// <param name="xmlNode"></param> /// <returns></returns> public static string ReadNodeText(XmlNode xmlNode) { if (xmlNode == null) { return null; } return xmlNode.InnerText; } #endregion //========================================================= // }
願望本文所述對年夜家的C#法式設計有所贊助。