XML序列化和反序列化相关
2010-02-24 10:32 爱研究源码的javaer 阅读(239) 评论(0) 编辑 收藏 举报
代码
public class Config
{
private bool enabled = true;
public bool Enabled { get { return enabled; } set { enabled = value; } }
private string serverUrl = "";
public string ServerUrl { get { return serverUrl; } set { serverUrl = value; } }
private UpdateFileList updateFileList = new UpdateFileList();
public UpdateFileList UpdateFileList
{
get { return updateFileList; }
set { updateFileList = value; }
}
public static Config LoadConfig(string file)
{
XmlSerializer xs = new XmlSerializer(typeof(Config));
StreamReader sr = new StreamReader(file);
Config config = xs.Deserialize(sr) as Config;
sr.Close();
return config;
}
public void SaveConfig(string file)
{
XmlSerializer xs = new XmlSerializer(typeof(Config));
StreamWriter sw = new StreamWriter(file);
xs.Serialize(sw, this);
sw.Close();
}
}
{
private bool enabled = true;
public bool Enabled { get { return enabled; } set { enabled = value; } }
private string serverUrl = "";
public string ServerUrl { get { return serverUrl; } set { serverUrl = value; } }
private UpdateFileList updateFileList = new UpdateFileList();
public UpdateFileList UpdateFileList
{
get { return updateFileList; }
set { updateFileList = value; }
}
public static Config LoadConfig(string file)
{
XmlSerializer xs = new XmlSerializer(typeof(Config));
StreamReader sr = new StreamReader(file);
Config config = xs.Deserialize(sr) as Config;
sr.Close();
return config;
}
public void SaveConfig(string file)
{
XmlSerializer xs = new XmlSerializer(typeof(Config));
StreamWriter sw = new StreamWriter(file);
xs.Serialize(sw, this);
sw.Close();
}
}
config = Config.LoadConfig(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FILENAME));
config是个类
主要在静态方法LoadConfig中
代码
public static Config LoadConfig(string file)
{
XmlSerializer xs = new XmlSerializer(typeof(Config));
StreamReader sr = new StreamReader(file);
Config config = xs.Deserialize(sr) as Config;
sr.Close();
return config;
}
{
XmlSerializer xs = new XmlSerializer(typeof(Config));
StreamReader sr = new StreamReader(file);
Config config = xs.Deserialize(sr) as Config;
sr.Close();
return config;
}
用到了XML序列化和反序列化