/// <summary> /// Reading Default Connection String From AppSettings Xml File /// </summary> /// <param name="fileName"></param> /// <returns type="DataSettings"></returns> public static DataSettings ReadConnectionStringFromXml(string fileName) { try { if (string.IsNullOrEmpty(fileName)) throw new ArgumentNullException("fileName"); DataSettings dataSettings = new DataSettings(); //creating new XML document XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(fileName); XmlNode dataConnectionStringNode = xmlDoc.SelectSingleNode("/connectionStrings/connectionString/dataConnectionString"); string dataConnectionString = dataConnectionStringNode.InnerText; XmlNode dataProviderNameNode = xmlDoc.SelectSingleNode("/connectionStrings/connectionString/dataProviderName"); string dataProviderName = dataProviderNameNode.InnerText; dataSettings.DataConnectionString = String.IsNullOrEmpty(dataConnectionString) ? null : dataConnectionString; dataSettings.DataProvider = String.IsNullOrEmpty(dataProviderName) ? null : dataProviderName; return dataSettings; } catch (Exception ex) { throw ex; } }
/// <summary> /// Parsing Connection String To DataSettings From Text File /// </summary> /// <param name="text"></param> /// <returns type="DataSettings"></returns> public static DataSettings ParseDataSettingsFromText(string text) { var dataSettings = new DataSettings(); if (String.IsNullOrEmpty(text)) return dataSettings; //Old way of file reading. This leads to unexpected behavior when a user's FTP program transfers these files as ASCII (\r\n becomes \n). //var settings = text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); var settings = new List<string>(); using (var reader = new StringReader(text)) { string str; while ((str = reader.ReadLine()) != null) settings.Add(str); } foreach (var setting in settings) { var separatorIndex = setting.IndexOf(":"); if (separatorIndex == -1) { continue; } string key = setting.Substring(0, separatorIndex).Trim(); string value = setting.Substring(separatorIndex + 1).Trim(); switch (key) { case "DataProvider": dataSettings.DataProvider = value; break; case "DataConnectionString": dataSettings.DataConnectionString = value; break; default: dataSettings.RawDataSettings.Add(key, value); break; } } return dataSettings; }
/// <summary> /// Parsing Connection String To DataSettings From WebConfig File /// </summary> /// <param name="dataProvider"></param> /// <param name="dataConnectionString"></param> /// <returns type="DataSettings"></returns> public static DataSettings ParseDataSettingsFromWebConfig(string dataProvider, string dataConnectionString) { var dataSettings = new DataSettings(); if (String.IsNullOrEmpty(dataProvider) && String.IsNullOrEmpty(dataConnectionString)) return dataSettings; dataSettings.DataProvider = dataProvider; dataSettings.DataConnectionString = dataConnectionString; dataSettings.RawDataSettings.Add("DataProvider", dataProvider); dataSettings.RawDataSettings.Add("DataConnectionString", dataConnectionString); return dataSettings; }