This article describes the implementation of XML file reading tool class based on C#. Share it for your reference. The details are as follows:
This is an XML file reading tool class I wrote last year, and now I have made some adjustments
It can basically meet the general XML file reading requirements.
renew:
June 26, 2013 19:37 Fixed several bugs, added unit tests for all methods, and ran through them; at the same time, the location of several methods was adjusted.
/// <summary> /// Author: jiangxiaoqiang /// </summary> public class XmlReader { //========================================================= // #region Get XmlDocument object /// <summary> /// Get XmlDocument object based on XML file content /// </summary> /// <param name="xmlFileContent"></param> /// <returns></returns> public static XmlDocument GetXmlDocByXmlContent(string xmlFileContent) { if ((xmlFileContent)) { return null; } var xDoc = new XmlDocument(); try { (xmlFileContent); } catch { xDoc = null; } return xDoc; } /// <summary> /// Get XmlDocument object according to XML file path /// </summary> /// <param name="xmlFilePath"></param> /// <returns></returns> public static XmlDocument GetXmlDocByFilePath(string xmlFilePath) { if ((xmlFilePath) || !(xmlFilePath)) { return null; } var xDoc = new XmlDocument(); try { (xmlFilePath); } catch { throw new Exception(("Please confirm thisXMLThe file format is correct,The path is:{0}", xmlFilePath)); } return xDoc; } #endregion //========================================================= // //========================================================= // #region Get XML nodes (or node list) /// <summary> /// Get the first child node with the specified node name under the parent node /// </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 && > 0) { return childXmlNodes[0]; } return null; } /// <summary> /// Get the list of child nodes with specified node name under the parent node /// </summary> /// <param name="parentXmlNode">parent node</param> /// <param name="nodeName">Node name</param> /// <returns></returns> public static XmlNodeList GetChildNodesByName(XmlNode parentXmlNode, string nodeName) { if (parentXmlNode == null || (nodeName)) { return null; } return GetChildNodesByXPathExpr(parentXmlNode, (".//{0}", nodeName)); } /// <summary> /// Get the list of XML child nodes that satisfy the xpathExpr expression under the parent node /// </summary> /// <param name="parentXmlNode">parent node</param> /// <param name="xpathExpr"></param> /// <returns></returns> public static XmlNodeList GetChildNodesByXPathExpr(XmlNode parentXmlNode, string xpathExpr) { if (parentXmlNode == null || (xpathExpr)) { return null; } return (xpathExpr); } /// <summary> /// Get the first child node under the parent node /// </summary> /// <param name="parentXmlNode"></param> /// <returns></returns> public static XmlNode GetFirstChildNode(XmlNode parentXmlNode) { var childXmlNodes = GetChildNodes(parentXmlNode); if (childXmlNodes != null && > 0) { return childXmlNodes[0]; } return null; } /// <summary> /// Get the parent node's child node list /// </summary> /// <param name="parentXmlNode">parent node</param> /// <returns></returns> public static XmlNodeList GetChildNodes(XmlNode parentXmlNode) { return parentXmlNode == null ? null : ; } #endregion //========================================================= // //========================================================= // #region Read node attribute value /// <summary> /// Read the attribute value of an XML node (based on the attribute name) /// </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 : (attrName); } /// <summary> /// Read the attribute value of the first child node that specifies the node name and attribute name under the parent node /// </summary> /// <param name="parentXmlNode">XML parent node</param> /// <param name="childNodeName">Nodename</param> /// <param name="attrName">Attribute name</param> /// <returns></returns> public static string ReadFirstAttrValue(XmlNode parentXmlNode, string childNodeName, string attrName) { var attrVals = ReadAttrValues(parentXmlNode, childNodeName, attrName); return (attrVals == null || == 0) ? null : attrVals[0]; } /// <summary> /// Read an array of the attribute values of all children of the specified node name and attribute name under the parent node /// </summary> /// <param name="parentXmlNode">XML Document</param> /// <param name="childNodeName">Nodename</param> /// <param name="attrName">Attribute name</param> /// <returns></returns> public static string[] ReadAttrValues(XmlNode parentXmlNode, string childNodeName, string attrName) { if (parentXmlNode == null || (childNodeName) || (attrName)) { return null; } var xpathExpr = ("//{0}[@{1}]", childNodeName, attrName); var nodes = GetChildNodesByXPathExpr(parentXmlNode, xpathExpr); if (nodes != null && > 0) { var nodeCount = ; 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 Reads the text content of the child node under the parent node /// <summary> /// Read the text of the first child node with the specified node name under the parent node /// </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 && > 0) { return childNodeTexts[0]; } return null; } /// <summary> /// Read the text array of all children with specified node names under the parent node /// </summary> /// <param name="parentXmlNode"></param> /// <param name="childNodeName"></param> /// <returns></returns> public static string[] ReadChildNodeTextsByName(XmlNode parentXmlNode, string childNodeName) { if (parentXmlNode == null || (childNodeName)) { return null; } var xpathExpr = (".//{0}", childNodeName); var childNodes = GetChildNodesByXPathExpr(parentXmlNode, xpathExpr); if (childNodes != null && > 0) { var nodeCount = ; var nodeTexts = new string[nodeCount]; for (var i = 0; i < nodeCount; i++) { nodeTexts[i] = childNodes[i].InnerText; } return nodeTexts; } return null; } /// <summary> /// Read the text of the first child node under the parent node /// </summary> /// <param name="parentXmlNode"></param> /// <returns></returns> public static string ReadFirstChildNodeText(XmlNode parentXmlNode) { var childNodeTexts = ReadChildNodeTexts(parentXmlNode); if (childNodeTexts != null && > 0) { return childNodeTexts[0]; } return null; } /// <summary> /// Read the text array of all children under the parent node /// </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 && > 0) { var nodeCount = ; var nodeTexts = new string[nodeCount]; for (var i = 0; i < nodeCount; i++) { nodeTexts[i] = childNodes[i].InnerText; } return nodeTexts; } return null; } /// <summary> /// Read XML node text /// </summary> /// <param name="xmlNode"></param> /// <returns></returns> public static string ReadNodeText(XmlNode xmlNode) { if (xmlNode == null) { return null; } return ; } #endregion //========================================================= // }
I hope this article will be helpful to everyone's C# programming.