Realization of XML file reading tool class based on C

  • 2021-07-06 11:38:37
  • OfStack

This paper 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 reading requirements of XML files.
Update:
On June 26, 2013, at 19:37 Fix, several Bug were added to unit tests for all methods and ran through; At the same time, the positions of several methods were adjusted.


/// <summary>
/// Author: jiangxiaoqiang
/// </summary>
public class XmlReader
{
  //========================================================= //
  #region  Get XmlDocument Object 
  /// <summary>
  ///  According to XML File content acquisition XmlDocument Object 
  /// </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>
  ///  According to XML File path acquisition XmlDocument Object 
  /// </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(" Please confirm that the XML The file format is correct and the path is: {0}", xmlFilePath));
    }
    return xDoc;
  }
  #endregion
  //========================================================= //
  //========================================================= //
  #region  Get XML Node (or node list) 
  /// <summary>
  ///  Gets the number of the specified node name under the parent node 1 Child 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 && childXmlNodes.Count > 0)
    {
      return childXmlNodes[0];
    }
    return null;
  }
  /// <summary>
  ///  Gets the list of child nodes with the 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 || string.IsNullOrEmpty(nodeName))
    {
      return null;
    }
    return GetChildNodesByXPathExpr(parentXmlNode, string.Format(".//{0}", nodeName));
  }
  /// <summary>
  ///  Gets the satisfaction under the parent node xpathExpr Of the expression XML Child node list 
  /// </summary>
  /// <param name="parentXmlNode"> Parent node </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>
  ///  Gets the first under the parent node 1 Child node 
  /// </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>
  ///  Gets the list of children of the parent node 
  /// </summary>
  /// <param name="parentXmlNode"> Parent node </param>
  /// <returns></returns>
  public static XmlNodeList GetChildNodes(XmlNode parentXmlNode)
  {
    return parentXmlNode == null ? null : parentXmlNode.ChildNodes;
  }
  #endregion
  //========================================================= //
  //========================================================= //
  #region  Read Node Attribute Values 
  /// <summary>
  ///  Read a XML Attribute value of node (according to 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 : xmlElement.GetAttribute(attrName);
  }
  /// <summary>
  ///  Reads the number of the specified node name and attribute name under the parent node 1 Attribute values of child nodes 
  /// </summary>
  /// <param name="parentXmlNode">XML Parent node </param>
  /// <param name="childNodeName"> Node name </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 || attrVals.Length == 0) ? null : attrVals[0];
  }
  /// <summary>
  ///  Reads an array of the attribute values for all children of the specified node name and attribute name under the parent node 
  /// </summary>
  /// <param name="parentXmlNode">XML Document </param>
  /// <param name="childNodeName"> Node name </param>
  /// <param name="attrName"> Attribute name </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  Read the text content of the child node under the parent node 
  /// <summary>
  ///  Reads the number of the specified node name under the parent node 1 Text of child nodes 
  /// </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>
  ///  Reads the text array of all children of the specified node name 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 || 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>
  ///  Read the first under the parent node 1 Text of child nodes 
  /// </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>
  ///  Read the text array of all child nodes 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 && 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>
  ///  Read XML Node text 
  /// </summary>
  /// <param name="xmlNode"></param>
  /// <returns></returns>
  public static string ReadNodeText(XmlNode xmlNode)
  {
    if (xmlNode == null)
    {
      return null;
    }
    return xmlNode.InnerText;
  }
  #endregion
  //========================================================= //
}

I hope this article is helpful to everyone's C # programming.


Related articles: