SoFunction
Updated on 2025-03-06

Implement XML file reading tool class based on C#

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>  /// &lt;returns&gt;&lt;/returns&gt;
  public static XmlNodeList GetChildNodesByName(XmlNode parentXmlNode, string nodeName)
  {
    if (parentXmlNode == null || (nodeName))
    {
      return null;
    }
    return GetChildNodesByXPathExpr(parentXmlNode, (".//{0}", nodeName));
  }
  /// &lt;summary&gt;
  /// Get the list of XML child nodes that satisfy the xpathExpr expression under the parent node  /// &lt;/summary&gt;
  /// <param name="parentXmlNode">parent node</param>  /// &lt;param name="xpathExpr"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;  
  public static XmlNodeList GetChildNodesByXPathExpr(XmlNode parentXmlNode, string xpathExpr)
  {
    if (parentXmlNode == null || (xpathExpr))
    {
      return null;
    }
    return (xpathExpr);
  }
  /// &lt;summary&gt;
  /// Get the first child node under the parent node  /// &lt;/summary&gt;
  /// &lt;param name="parentXmlNode"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public static XmlNode GetFirstChildNode(XmlNode parentXmlNode)
  {
    var childXmlNodes = GetChildNodes(parentXmlNode);
    if (childXmlNodes != null &amp;&amp;  &gt; 0)
    {
      return childXmlNodes[0];
    }
    return null;
  }
  /// &lt;summary&gt;
  /// Get the parent node's child node list  /// &lt;/summary&gt;
  /// <param name="parentXmlNode">parent node</param>  /// &lt;returns&gt;&lt;/returns&gt;
  public static XmlNodeList GetChildNodes(XmlNode parentXmlNode)
  {
    return parentXmlNode == null ? null : ;
  }
  #endregion
  //========================================================= //
  //========================================================= //
  #region Read node attribute value  /// &lt;summary&gt;
  /// Read the attribute value of an XML node (based on the attribute name)  /// &lt;/summary&gt;
  /// &lt;param name="xmlNode"&gt;&lt;/param&gt;
  /// &lt;param name="attrName"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public static string ReadAttrValue(XmlNode xmlNode, string attrName)
  {
    var xmlElement = xmlNode as XmlElement;
    return xmlElement == null ? null : (attrName);
  }
  /// &lt;summary&gt;
  /// Read the attribute value of the first child node that specifies the node name and attribute name under the parent node  /// &lt;/summary&gt;
  /// <param name="parentXmlNode">XML parent node</param>  /// <param name="childNodeName">Nodename</param>  /// <param name="attrName">Attribute name</param>  /// &lt;returns&gt;&lt;/returns&gt;
  public static string ReadFirstAttrValue(XmlNode parentXmlNode, string childNodeName, string attrName)
  {
    var attrVals = ReadAttrValues(parentXmlNode, childNodeName, attrName);
    return (attrVals == null ||  == 0) ? null : attrVals[0];
  }
  /// &lt;summary&gt;
  /// Read an array of the attribute values ​​of all children of the specified node name and attribute name under the parent node  /// &lt;/summary&gt;
  /// <param name="parentXmlNode">XML Document</param>  /// <param name="childNodeName">Nodename</param>  /// <param name="attrName">Attribute name</param>  /// &lt;returns&gt;&lt;/returns&gt;
  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 &amp;&amp;  &gt; 0)
    {
      var nodeCount = ;
      var attrVals = new string[nodeCount];
      for (var i = 0; i &lt; 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  /// &lt;summary&gt;
  /// Read the text of the first child node with the specified node name under the parent node  /// &lt;/summary&gt;
  /// &lt;param name="parentXmlNode"&gt;&lt;/param&gt;
  /// &lt;param name="childNodeName"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public static string ReadFirstChildNodeTextByName(XmlNode parentXmlNode, string childNodeName)
  {
    var childNodeTexts = ReadChildNodeTextsByName(parentXmlNode, childNodeName);
    if (childNodeTexts != null &amp;&amp;  &gt; 0)
    {
      return childNodeTexts[0];
    }
    return null;
  }
  /// &lt;summary&gt;
  /// Read the text array of all children with specified node names under the parent node  /// &lt;/summary&gt;
  /// &lt;param name="parentXmlNode"&gt;&lt;/param&gt;
  /// &lt;param name="childNodeName"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  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 &amp;&amp;  &gt; 0)
    {
      var nodeCount = ;
      var nodeTexts = new string[nodeCount];
      for (var i = 0; i &lt; nodeCount; i++)
      {
        nodeTexts[i] = childNodes[i].InnerText;
      }
      return nodeTexts;
    }
    return null;
  }
  /// &lt;summary&gt;
  /// Read the text of the first child node under the parent node  /// &lt;/summary&gt;
  /// &lt;param name="parentXmlNode"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public static string ReadFirstChildNodeText(XmlNode parentXmlNode)
  {
    var childNodeTexts = ReadChildNodeTexts(parentXmlNode);
    if (childNodeTexts != null &amp;&amp;  &gt; 0)
    {
      return childNodeTexts[0];
    }
    return null;
  }
  /// &lt;summary&gt;
  /// Read the text array of all children under the parent node  /// &lt;/summary&gt;
  /// &lt;param name="parentXmlNode"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public static string[] ReadChildNodeTexts(XmlNode parentXmlNode)
  {
    if (parentXmlNode == null)
    {
      return null;
    }
    var childNodes = GetChildNodes(parentXmlNode);
    if (childNodes != null &amp;&amp;  &gt; 0)
    {
      var nodeCount = ;
      var nodeTexts = new string[nodeCount];
      for (var i = 0; i &lt; nodeCount; i++)
      {
        nodeTexts[i] = childNodes[i].InnerText;
      }
      return nodeTexts;
    }
    return null;
  }
  /// &lt;summary&gt;
  /// Read XML node text  /// &lt;/summary&gt;
  /// &lt;param name="xmlNode"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  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.