public ConfigurationSection GetSection(string sectionName) { ConfigurationSection configSection; if (sections.TryGetValue(sectionName, out configSection)) { SerializableConfigurationSection section = configSection as SerializableConfigurationSection; if (section != null) { using (StringWriter xml = new StringWriter()) using (XmlWriter xmlwriter = XmlWriter.Create(xml)) { section.WriteXml(xmlwriter); xmlwriter.Flush(); MethodInfo methodInfo = section.GetType().GetMethod("DeserializeSection", BindingFlags.NonPublic | BindingFlags.Instance); methodInfo.Invoke(section, new object[] { XDocument.Parse(xml.ToString()).CreateReader() }); return(configSection); } } } return(null); }
/// <summary> /// /// </summary> public void SaveSection(string sectionName, SerializableConfigurationSection configurationSection) { //TODO: if encryption enabled, encrypt it here // Create Instance of Connection and Command Object using (SqlConnection myConnection = new SqlConnection(data.ConnectionString)) { try { SqlCommand myCommand = new SqlCommand(data.SetStoredProcedure, myConnection); myCommand.CommandType = CommandType.StoredProcedure; SqlParameter sectionNameParameter = new SqlParameter(@"@section_name", SqlDbType.NVarChar); sectionNameParameter.Value = sectionName; myCommand.Parameters.Add(sectionNameParameter); SqlParameter sectionTypeParameter = new SqlParameter(@"@section_type", SqlDbType.NVarChar); sectionTypeParameter.Value = configurationSection.GetType().AssemblyQualifiedName; myCommand.Parameters.Add(sectionTypeParameter); SqlParameter sectionValueParameter = new SqlParameter(@"@section_value", SqlDbType.NText); StringBuilder output = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); using (XmlWriter writer = XmlWriter.Create(output, settings)) { configurationSection.WriteXml(writer); writer.Close(); writer.Flush(); } sectionValueParameter.Value = output.ToString(); myCommand.Parameters.Add(sectionValueParameter); // Execute the command myConnection.Open(); myCommand.ExecuteNonQuery(); } catch (Exception e) { throw new ConfigurationErrorsException(Resources.ExceptionConfigurationCannotSet, e); } } }