以下に示すように、いくつかのクラスからXMLファイルを簡単に生成することができました。
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");
}
これは完璧に機能し、私に次のようになります。
<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>
ただし、これをフォームに戻すにはどうすればよいですか?だから私はこのようなものを持っていますが、それを機能させることができないようです。
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.
2つの構成クラスをロードしてから、それらを全体的な構成クラスに割り当てる必要がありますか?または、クラス全体をロードして、サブ構成クラスをこれから割り当てる必要がありますか?
allConfig = new AllConfig();
dbConfig = new DataBaseConfiguration();
merlinConfig = new MerlinConfiguration();
allConfig.databaseConfiguration = dbConfig;
allConfig.merlinConfiguration = merlinConfig;
allConfig.databaseConfiguration.Load();
allConfig.merlinConfiguration.Load();
編集:ここに私の読み込み方法があります。
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();
}
}