I’ve managed to easily generate an XML file from a few classes, as shown below;
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");
}
This works perfectly and gives me:
<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>
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;
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.
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;
allConfig = new AllConfig();
dbConfig = new DataBaseConfiguration();
merlinConfig = new MerlinConfiguration();
allConfig.databaseConfiguration = dbConfig;
allConfig.merlinConfiguration = merlinConfig;
allConfig.databaseConfiguration.Load();
allConfig.merlinConfiguration.Load();
edit: heres my loading method;
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();
}
}
Fixed my own problem guys. It was how i was loading it, in the end i ended up doing it like so;
so i created an instance of the class i serialise, then created the classes which is consists off, and assigned these to the main class. Then i loaded the main class (which i assume through the serializer populates the classes it consists off, and then i accessed them like so;
and so on. thanks for the advice though guys, really helped me out.