deserialising XML correctly into classes

chakru

New Member
I've managed to easily generate an XML file from a few classes, as shown below;\[code\]public class AllConfig : XMLEncapsulator{ [XmlElement("Database-Settings")] public DataBaseConfiguration databaseConfiguration { get; set; } [XmlElement("Merlin-Settings")] public MerlinConfiguration merlinConfiguration { get; set; }}public class DataBaseConfiguration : XMLEncapsulator{ public string dbIP { get; set; } public int ?port { get; set; } public string username { get; set; } public string password { get; set; }}public class MerlinConfiguration : XMLEncapsulator{ public string MerlinIP { get; set; } public int ?MerlinPort { get; set; } public int ?RecievingPort { get; set; }}// load classes with information, then; try { allConfig.databaseConfiguration = dbConfig; allConfig.merlinConfiguration = merlinConfig; allConfig.Save(); } catch (Exception ErrorFinalisingSave) { MessageBox.Show(ErrorFinalisingSave.Message + "3"); }\[/code\]This works perfectly and gives me:\[code\]<AllConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <databaseConfiguration> <dbIP></dbIP> <port></port> <username></username> <password></password></databaseConfiguration><merlinConfiguration> <MerlinIP></MerlinIP> <MerlinPort></MerlinPort> <RecievingPort></RecievingPort> </merlinConfiguration></AllConfig>\[/code\]However, how do i go about retrieving this back into my forms? so i've got something like this, but i can't seem to get it to work;\[code\] AllConfig allConfig; DataBaseConfiguration dbConfig; MerlinConfiguration merlinConfig; //need to load here.//check if values loaded are null, and then load them if they exist into textboxes and such.\[/code\]should i load the two config classes and then assign them to the overall config class? or do i need to load the overall class and assign the sub-config classes off this, like so;\[code\] allConfig = new AllConfig(); dbConfig = new DataBaseConfiguration(); merlinConfig = new MerlinConfiguration(); allConfig.databaseConfiguration = dbConfig; allConfig.merlinConfiguration = merlinConfig; allConfig.databaseConfiguration.Load(); allConfig.merlinConfiguration.Load();\[/code\]edit: heres my loading method;\[code\] public virtual void Load(){ if (File.Exists(DeviceManager.path)) { StreamReader sr = new StreamReader(DeviceManager.path); XmlTextReader xr = new XmlTextReader(sr); XmlSerializer xs = new XmlSerializer(this.GetType()); object c; if (xs.CanDeserialize(xr)) { c = xs.Deserialize(xr); Type t = this.GetType(); PropertyInfo[] properties = t.GetProperties(); foreach (PropertyInfo p in properties) { p.SetValue(this, p.GetValue(c, null), null); } } xr.Close(); sr.Close(); }}\[/code\]
 
Back
Top