我正在使用C#和.NET 3.5框架編寫應用程序。幫助使用帶有枚舉的LINQ和XML
我有一個這樣的枚舉,
public static enum SettingOneType { FooA, FooB, FooC }
我也有,我加載像這樣的load()方法一個XDocument,
LoadXML(){
...
XDocument SettingsDocument;
if(File.Exists(path)
{
XElement SettingsElement = new XElement("DeviceSettings",
new XElement("Setitng1", SettingOneType.FooA.ToString()),
new XElement("Setting2", ... ));
XDeclaration dec = new XDeclaration("1.0", "UTF-8", "yes");
SettingsDocument = new XDocument(dec, SettingsElement);
SettingsDocument.Save(xpath);
}
else SettingsDocument = XDocument.Load(path);
}
我想知道是,是否有一種以強類型方式讀取這些設置的方法。因爲我想在我的應用程序中的屬性,將在這樣的xml文件訪問值...
public SettingOneType SettingOne
{
get{
SettingOneType x = SettingsDocument. //Here I know I can use LINQ statements to file the value I want but is there a way to cast the value to the correct type without using a giant switch statement or something?
}
}
注:之前有人建議我使用內置的可用應用設置文件。不要打擾。我通常使用這些,但是對於這個項目,我有一個理由,我不能解釋。
因此,這將讀取並保存所有的設置一次? – PICyourBrain 2010-09-14 14:29:50
是的。我也建議這樣做,除非你有大量的數據,否則它比其他選項更容易 - 那麼也許應該考慮一個替代方案。我的意思是巨大 - XML序列化是_quite_性能。 – 2010-09-14 17:51:49
約旦:是的。更新單個設置通常涉及讀取整個類,更改值並將所有內容序列化迴文件。對於小型課程來說,開銷是相當小的,但顯然如果你的設置文件很大,那麼這不是最好的方法。 – Addys 2010-09-14 20:14:13