我正在使用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