【发布时间】:2016-01-08 12:38:51
【问题描述】:
我创建了一个名为“VNCVars.cs”的类,我希望能够将与 VNCVars 中的所有变量关联的数据保存到 XML 文件中,以便在启动时将它们重新加载。我有另一个名为“SaveData.cs”的类,其中包含生成 XML 文件的代码。
我编写了可以运行的代码,但我的 XML 文件始终为空.....有人能指出我错过了什么吗??
public class VNCVars
{
//Global Variables for VNC 1 Location
//VNC File Location 1 Get and Set routines
private static string strVNC1Location;
public static string VNC1Location
{
get { return strVNC1Location; }
set { strVNC1Location = value; }
}
//Global Variables for VNC 2 Location
//VNC File Location 2 Get and Set routines
private static string strVNC2Location;
public static string VNC2Location
{
get { return strVNC2Location; }
set { strVNC2Location = value; }
}
//Global Variables for VNC 3 Location
//VNC File Location 3 Get and Set routines
private static string strVNC3Location;
public static string VNC3Location
{
get { return strVNC3Location; }
set { strVNC3Location = value; }
}
}
public class SaveXML
{
public static void SaveData()
{
var SaveData = new VNCVars();
XmlSerializer sr = new XmlSerializer(typeof(VNCVars));
TextWriter writer = new StreamWriter(@"c:\Fanuc\SetupVars.xml");
sr.Serialize(writer, SaveData);
writer.Close();
}
}
最后在我的表单上,我目前只有一个按钮,点击后会出现以下情况...
private void button2_Click(object sender, EventArgs e)
{
SaveXML.SaveData();
}
非常感谢任何帮助....
【问题讨论】:
-
你不能使用这样的静态字段序列化一个类。使字段非静态。
-
我只是想说构造函数在哪里
-
如果我将它们设为非静态,那么我将无法从我拥有的每个表单(即 VNCVars.VNC1Location)中访问变量。对于基本问题,我很抱歉,但我对 C# 编程相当陌生。