Common Method Tool Classes for Reading and Writing Xml Configuration Files in C#

Keywords: C# xml Windows

scene

Sometimes it is necessary to use configuration files to save some configuration properties so that the settings will still take effect the next time they are opened.

Here is an example of reading and writing xml configuration files.

1. Read the XML configuration.

2. Write the XML configuration file.

3. The first XmlNode that matches the XPath expression.

4. Get the node text.

5. Get the number of nodes according to xPath.

6. Get the list of nodes according to xPath.

The above functions are only based on their own actual business encapsulation methods, other methods can be based on their own actual business encapsulation.

Realization

Create a new xml configuration file in the project directory.

 

 

Code:

class ReadXMLConfigHelper
    {
        #region read XML configuration file
        /// <summary>
        /// read XML Configuration file failed to return null
        /// </summary>
        /// <param name="filePath">xml File path, relative to bin lower debug Catalog</param>
        /// <returns>xml Document object</returns>
        public static XmlDocument readXml(string filePath)
        {
            //Get the path to the executable file-Namely bin Directory debug perhaps release Catalog
            string context = System.Windows.Forms.Application.StartupPath;
            //Stitching Full Path
            string path = String.Concat(context, filePath);
            XmlDocument xml = new XmlDocument();
            //Open one xml
            try
            {
                xml.Load(path);
                return xml;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }
        #endregion

        #region Write in XML configuration file
        /// <summary>
        ///  Write in XML Configuration file returned successfully True Failure to return false
        /// </summary>
        /// <param name="xml">xml object</param>
        /// <param name="filePath">File path</param>
        /// <returns></returns>
        public static Boolean  writeXml(XmlDocument xml , string filePath)
        {
            //Get the path to the executable file-Namely bin Directory debug perhaps release Catalog
            string context = System.Windows.Forms.Application.StartupPath;
            //Stitching Full Path
            string path = String.Concat(context, filePath);
            try
            {
                xml.Save(path);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }
        #endregion

        #region matching XPath The first expression XmlNode
        /// <summary>
        /// matching XPath The first expression XmlNode
        /// </summary>
        /// <param name="xml">xml Document object</param>
        /// <param name="xPath">xPath-Path Matching Expressions</param>
        /// <returns>xml Failed return of node object Null</returns>
        public static XmlNode getXmlNode(XmlDocument xml,string xPath)
        {
             //Selection matching XPath The first expression XmlNode
            XmlNode xmlNode = xml.SelectSingleNode(xPath);
            //Read node data
            if (xmlNode != null)
            {
               return xmlNode;
            }
            else 
            {
               return null;
            }
        }
        #endregion

        #region Get node text
        /// <summary>
        /// Get node text
        /// </summary>
        /// <param name="xml Get node textNode">Node object</param>
        /// <returns>Return null Fail, return""Represents that the content of the node is empty and returns to the node successfully. text</returns>
        public static string getNodeText(XmlNode xmlNode)
        {
            //Read node data
            if (xmlNode != null)
            {
                string nodeText = xmlNode.InnerText;
                if (nodeText != null)
                {
                    return nodeText;
                }
                else
                {
                    return "";
                }
            }
            else 
            {
               return null;
            }
        }
        #endregion

        #region according to xPath Get the number of nodes
        /// <summary>
        /// according to xPath Get the number of nodes
        /// </summary>
        /// <param name="xml">xml Document object</param>
        /// <param name="xPath">xPath Expression</param>
        /// <returns>Return coincidence xPath The number of nodes, if not, returns 0</returns>
         public static int getCountByXpath(XmlDocument xml,string xPath)
         {
             //Read node list
             XmlNodeList nodelist = xml.SelectNodes(xPath);
             if (nodelist != null)
             {
                 return nodelist.Count;
             }
             else
             {
                 return 0;
             }
         }
        #endregion

        #region according to xPath Get the list of nodes
         /// <summary>
         ///  according to xPath Get the list of nodes
         /// </summary>
         /// <param name="xml">xml Document object</param>
         /// <param name="xPath">xPath Expression</param>
         /// <returns>Return coincidence xPath List of nodes, failed return null</returns>
         public static XmlNodeList getNodeListByXpath(XmlDocument xml, string xPath)
         {
             //Read node list
             XmlNodeList nodelist = xml.SelectNodes(xPath);
             if (nodelist != null)
             {
                 return nodelist;
             }
             else
             {
                 return null;
             }
         }
         #endregion

    }

 

Call examples:

XmlDocument xml = ReadXMLConfigHelper.readXml(xmlFilePath);

XmlNodeList nodeList = ReadXMLConfigHelper.getNodeListByXpath(xml, Global.RADIO_GROUP_COUNT_XPATH);

Posted by harlequeen on Wed, 09 Oct 2019 07:00:09 -0700