Hi there
AppSettings Class :
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
[Serializable(), XmlRoot("appSettings")]
public class AppSettings
{
private List<Settings> settings = new List<Settings>();
[XmlElement("setting")]
public List<Settings> Settings
{
get { return settings; }
set { settings = value; }
}
public static AppSettings Load(string filename)
{
AppSettings appSettings = null;
XmlSerializer ser = new XmlSerializer(typeof(AppSettings));
FileStream fstream = null;
StreamReader sreader = null;
try
{
fstream = new FileStream(filename, FileMode.Open, FileAccess.Read);
sreader = new StreamReader(fstream);
appSettings = (AppSettings)ser.Deserialize(sreader);
return appSettings;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (sreader != null) sreader.Close();
if (fstream != null) fstream.Close();
}
}
public void Save(string filename)
{
XmlSerializer ser = new XmlSerializer(typeof(AppSettings));
FileStream fstream = null;
StreamWriter swriter = null;
try
{
fstream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
swriter = new StreamWriter(fstream);
ser.Serialize(swriter, this);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (swriter != null)
{
swriter.Flush();
swriter.Close();
}
if (fstream != null) fstream.Close();
}
}
}
Settings Base Class :
using System;
using System.Xml;
using System.Xml.Serialization;
[XmlInclude(typeof(ProxySettings)), XmlInclude(typeof(SecuritySettings))]
public class Settings
{
}
ProxySettings Class :
using System;
using System.Xml;
using System.Xml.Serialization;
[Serializable()]
public class ProxySettings : Settings
{
private string address;
private string username;
private string password;
public ProxySettings() { }
public ProxySettings(string address, string username, string password)
{
this.Address = address;
this.UserName = username;
this.Password = password;
}
[XmlAttribute("address")]
public string Address
{
get { return address; }
set { address = value; }
}
[XmlAttribute("username")]
public string UserName
{
get { return username; }
set { username = value; }
}
[XmlAttribute("password")]
public string Password
{
get { return password; }
set { password = value; }
}
}
SecuritySettings Class :
using System;
using System.Xml;
using System.Xml.Serialization;
[Serializable()]
public class SecuritySettings : Settings
{
private string passwordFormat;
private int minimumPasswordLength;
public SecuritySettings() { }
public SecuritySettings(string passwordFormat, int minimumPasswordLength)
{
this.PasswordFormat = passwordFormat;
this.MinimumPasswordLength = minimumPasswordLength;
}
[XmlAttribute("passwordFormat")]
public string PasswordFormat
{
get { return passwordFormat; }
set { passwordFormat = value; }
}
[XmlAttribute("minimumPasswordLength")]
public int MinimumPasswordLength
{
get { return minimumPasswordLength; }
set { minimumPasswordLength = value; }
}
}
Enter your message below
Sign in or Join us (it's free).