Tutorial: Retrieving XML Config file values
Share/Save/Bookmark

Description: This topic will show you how to go through a Configuration XML file and get the values for the Element you want.



1.  Inside of your project, add a "Config" folder.  

2.  Inside the "Config" folder, add new configurations file.

3.  Inside this Config file, put some XML in it.. Such as:

<xml version="1.0" encoding="utf-8"  ?>
   <configuration>
      <maildomain>mydomain</maildomain>
      <accounts>
         <account>
            <username>dave@covertcoder.com</username>
            <password>mypassword<password>
         </account>
         <account>
            <username>david@covertcoder.com</username>
            <password>mypassword</password>
         </account>
      </accounts>
   </configuration>

4.  Create a class within your project caled "MailAccountSettings" that you will use Globally within your application

   [Serializable]
   class MailAccountSettings
   {
      private static string _UserName;
      private static string _Password;

      static internal string UserName
      {
         get { return _UserName; }
         set { _UserName = value; }
      }

      static internal string Password
      {
         get { return _Password; }
         set { _Password = value; }
      }
   }

5.  Create a class object that will populate the class object from your XML configuration file.

   using System.Windows.Forms;
   using System.Xml;

   class MailServerSetup

   {

      static internal void SetMailServerConfigurations()

      {

         try

         {

            string StartupPath = Application.StarupPath;

            XmlTextreader MailSetupXML = new XmlTextreader(StartupPath + @"\Config\MailSetup.config");

            string xmlValue = string.Empty;
            while (MailSetupXML.Read())
            {
               switch (MailSetupXML.NodeType)
               {
                  case XmlNodeType.Element:
                     {
                        xmlValue = MailSetupXML.Name;
                        break;
                     }
                  case XmlNodeType.Text:
                     {
                        switch (xmlValue)
                        {
                           case "username":
                              {
                                 MailAccountSettings.UserName = MailSetupXML.Value;
                                 break;
                              }
                           case "password":
                              {
                                 MailAccountSettings.Password = MailSetupXML.Value;
                                 break;
                              }
                        }
                        break;
                     }
               }
            }
         }
         catch (Exception ex)
         {}
      }
   }

6.  To wire it all and to populate the class with your values from the configuration file, just call the Setup from your some initialization point within your application, like the _Load event, form constructor, Services _Startup method.

   public frmMailApplication()
   {
      MailServerSetup.SetMailServerConfigurations();
      InitializeComponent();
   }