Tuesday, 12 December 2017

Xml and webconfig file handling

<div dir="ltr" style="text-align: left;" trbidi="on">
<form id="form1" runat="server">
<script>
          
        </script>
    <br />
<div>
Text="&lt;% $Resources: MapRes, global_productName %&gt;"
        Text="&lt;%$  appsettings: gridPageSize %&gt;"
        <asp:dropdownlist id="DropDownList1" runat="server"></asp:dropdownlist>
    </div>
<br />
Text ='&lt;%# String.Format(HttpContext.GetGlobalResourceObject("MapRes", "parameterized_resourceKey").ToString(), "Every One") %&gt;'
  
        <asp:button id="Button1" onclick="Button1_Click" runat="server" text="Save">
        <asp:button id="SetSession" onclick="SetSession_Click" runat="server" text="SetSession">
        <asp:button id="EndSession" onclick="EndSession_Click" runat="server" text="EndSession">
    </asp:button></asp:button></asp:button></form>
public enum ConfigSettingName
    {
        mailConfig = 1,
        gridViewConfig
    }
    public enum ConfigAttribute
    {
        enableOutGoingMail=1,
        gridPageSize
    }
    public enum GlobalOption
    {
        paymentOption=1,
        statusOption,
        gridPageSizeOption
    }
    public enum AppSettingKey
    {
        enableMailSending=1,
        gridPageSize
    }


public partial class ResourcePlaying : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Page.DataBind();
                bind_ddl1();
                DateTime dt1 = new DateTime(1970, 01, 01);
                DateTime dt2 = DateTime.Now;
                Label3.Text = Convert.ToString((dt1 - dt2).TotalMilliseconds);
                Label3.Text = Label3.Text.Replace(".", "Hi");
            }
        }
        public void bind_ddl1()
        {
            //XmlDocument xdoc = new XmlDocument();
            //xdoc.Load(Server.MapPath("xml") + "\\default.xml");
            //string PreferenceXML = xdoc.InnerXml;
            //XmlNode paymentNode = xdoc.SelectSingleNode("globalOption");
            //XmlNodeList PaymentOption = paymentNode.SelectNodes("tab[@name='paymentOption']/option");
            //XmlNode statusNode = xdoc.SelectSingleNode("globalOption");
            //XmlNodeList statusOption = statusNode.SelectNodes("tab[@name='statusOption']/option");
            XmlNodeList statusOption = GetOptionNodeList(nameof(GlobalOption.gridPageSizeOption));
            DropDownList1.Items.Clear();
            foreach (XmlNode xnode in statusOption)
            {
                string name = xnode.Attributes["name"].Value;
                string value = xnode.Attributes["value"].Value;
                ListItem i = null;
                i = new ListItem(name, value);
                DropDownList1.Items.Add(i);
            }
            //DropDownList1.SelectedValue = GetConfigValueFromXML(nameof(ConfigSettingName.gridViewConfig),
            //                                                    nameof(ConfigAttribute.gridPageSize));
            DropDownList1.SelectedValue = GetAppSettingValue(nameof(AppSettingKey.gridPageSize));
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                // 1. Updating value in xml file
                // SetValueInXMLConfiguration(ConfigSettingName.gridViewConfig.ToString(),
                //                ConfigAttribute.gridPageSize.ToString(),
                //                DropDownList1.SelectedValue);
                // 2. Updating value in webconfig appsetting
                SetAppSettingValue(nameof(AppSettingKey.gridPageSize), DropDownList1.SelectedValue);
                if (IsMailingEnable())
                    Response.Write("Mail feature is enabled");
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString()); 
            }
        }
        private bool IsMailingEnable()
        {
            string val = string.Empty;
            //1. reading value from xml file
            val = GetConfigValueFromXML(nameof(ConfigSettingName.mailConfig), nameof(ConfigAttribute.enableOutGoingMail));
            //2. reading value from web.config file
            val = GetAppSettingValue(nameof(AppSettingKey.enableMailSending));
            bool staus = val == "1" ? true : false;
            return staus;
        }
        private void SetValueInXMLConfiguration(string settingName,string attributeName, string value, string fileType = "XML")
        {
            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(Server.MapPath("xml") + "\\default.xml");
                string PreferenceXML = xdoc.InnerXml;
                //XmlNode mailConfig = xdoc.SelectSingleNode("globalOption/tab[@name='mailConfig']/setting");
                XmlElement xmlElement = (XmlElement)xdoc.SelectSingleNode("globalOption/tab[@name='configuration']/setting[@name='"+ settingName + "']");
                //XmlNode a = mailConfig.SelectSingleNode("setting/@id='1']");           
                if (xmlElement != null)
                {
                    //formData.SetAttribute("allowOutGoingMail", "1"); // Set to new value.
                    //formData.Attributes["enableOutGoingMail"].Value = value;
                    xmlElement.Attributes[attributeName].Value = value;
                    //Response.Write(xmlElement.Attributes[attributeName].Value);
                }
                xdoc.Save(Server.MapPath("xml") + "\\default.xml");
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
        private string GetConfigValueFromXML(string settingName, string attributeName)
        {
            string attributeValue = string.Empty;
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(Server.MapPath("xml") + "\\default.xml");
            string PreferenceXML = xdoc.InnerXml;
            XmlElement xmlElement = (XmlElement)xdoc.SelectSingleNode("globalOption/tab[@name='configuration']/setting[@name='" + settingName + "']");
            if (xmlElement != null)
                attributeValue = xmlElement.Attributes[attributeName].Value;
            return attributeValue;
        }
       
        public string GetAppSettingValue(string key)
        {
            string value = string.Empty;
            value= ConfigurationManager.AppSettings[key];
            return value;
        }
        public void SetAppSettingValue(string key, string newValue, string fileType="WebConfig")
        {
           
            //Helps to open the Root level web.config file.
            Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");
            //Modifying the AppKey from AppValue to AppValue1
            webConfigApp.AppSettings.Settings[key].Value = newValue;
            //Save the Modified settings of AppSettings.
            webConfigApp.Save();
            //set value in aspx page directly
            //&lt;%$  appsettings: *KeyName * %&gt;
            //="&lt;%$ ConfigurationSettings.AppSettings["ImagePath"] %&gt;"
        }
        private XmlNodeList GetOptionNodeList(string optionField)
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(Server.MapPath("xml") + "\\default.xml");
            string PreferenceXML = xdoc.InnerXml;
            XmlNode node = xdoc.SelectSingleNode("globalOption");
            XmlNodeList nodelist = node.SelectNodes("tab[@name='" + optionField + "']/option");
            //XmlNode statusNode = xdoc.SelectSingleNode("globalOption");
            //XmlNodeList statusOption = statusNode.SelectNodes("tab[@name='statusOption']/option");
            return nodelist;
        }
        protected void SetSession_Click(object sender, EventArgs e)
        {
            Session["IsUserValid"] = "Avi";
            ErrorReporting.Log.Info("Session Started: Logged form login page", "aspx page", "SetSession_Click");
        }
        protected void EndSession_Click(object sender, EventArgs e)
        {
            ErrorReporting.Log.Info("Logged form asax Session_End", "aspx page", "EndSession_Click");
            Session.Remove("IsUserValid");
            Session.RemoveAll();
            Response.Redirect("listbox.aspx");
        }
    }

default.xml
<globaloption>
  <tab id="1" name="paymentOption">
    <option name="Cash" value="Cash">
    <option name="Online" value="Online">
  </option></option></tab>
  <tab id="2" name="configuration">
    <setting enableoutgoingmail="1" name="mailConfig">
    <setting gridpagesize="150" name="gridViewConfig">
  </setting></setting></tab>
  <tab id="3" name="statusOption">
    <option name="Enable" value="1">
    <option name="Disable" value="0">
  </option></option></tab>
  <tab id="4" name="gridPageSizeOption">
    <option name="10" value="10">
    <option name="20" value="20">
    <option name="30" value="30">
    <option name="40" value="40">
    <option name="50" value="50">
    <option name="100" value="100">
    <option name="150" value="150">
    <option name="200" value="200">
  </option></option></option></option></option></option></option></option></tab>
</globaloption>

webconfig
<configuration>
 <appsettings>
    <add key="enableMailSending" value="1">
    <add key="gridPageSize" value="20">
  </add></add></appsettings>
</configuration></div>

No comments:

Post a Comment