当我启动程序并单击“保存”按钮(如视频游戏中的那些按钮)时,我尝试这样做,它会从保存文件中加载变量。但是当我尝试加载变量时,只会弹出消息框(" Save Loaded!")并且XML不会覆盖我的变量。这是代码:
private void MainWindow_Load(object sender, EventArgs e)
{
XmlReader xReader = XmlReader.Create("C:\\Save\\Save1.xml");
while (xReader.Read())
{
if ((xReader.NodeType == XmlNodeType.Element) && (xReader.Name == "Variable"))
{
if (xReader.HasAttributes)
{
MessageBox.Show("Loaded a save!");
Vars.zombies = (Convert.ToDouble(xReader.GetAttribute("Variable","Zombies")));
Vars.infected = (Convert.ToDouble(xReader.GetAttribute("Variable","Infected")));
Vars.wolfs = (Convert.ToDouble(xReader.GetAttribute("Varaible","Wolfs")));
}
}
}
这是保存后加载程序的时间。 ^
private void SaveButton_Click(object sender, EventArgs e)
{
XmlTextWriter xWriter = new XmlTextWriter("C:\\Save\\Save1.xml", Encoding.UTF8);
xWriter.WriteStartDocument();
xWriter.Formatting = Formatting.Indented;
xWriter.WriteStartElement("Variables");
xWriter.WriteStartElement("Variable");
xWriter.WriteAttributeString("Zombies",Vars.zombies.ToString());
xWriter.WriteEndElement();
xWriter.WriteStartElement("Variable");
xWriter.WriteAttributeString("Infected",Vars.infected.ToString());
xWriter.WriteEndElement();
xWriter.WriteStartElement("Variable");
xWriter.WriteAttributeString("Wolfs",Vars.wolfs.ToString());
xWriter.WriteEndElement();
xWriter.WriteEndElement();
xWriter.Close();
}
这是保存变量^
的代码public static double infected = 0;
public static double zombies = 0;
public static double wolfs = 0;
最后在一个分离的类^
中的变量有谁能告诉我我做错了什么?保存工作完美,但程序只是没有加载变量。
答案 0 :(得分:0)
您可以尝试使用以下代码进行测试。
Vars.zombies = (Convert.ToDouble(xReader.GetAttribute("Zombies")));
Vars.infected = (Convert.ToDouble(xReader.GetAttribute("Infected")));
Vars.wolfs = (Convert.ToDouble(xReader.GetAttribute("Wolfs")));
在GetAttributes中,您只需要传递您尝试检索其值的属性的名称。第二个可选参数采用属性的名称空间URI。
答案 1 :(得分:0)
我将向您展示一种非常天真的方法,这对于一些变量是可以的。如果您需要存储更多数据,请考虑使用DataContractSerializer
等强大功能。
首先,我将开始从属性更改为值,您当前的实现需要在XML文件中进行更困难的搜索。
private static void Save()
{
var xWriter = new XmlTextWriter("Save1.xml", Encoding.UTF8);
xWriter.WriteStartDocument();
xWriter.Formatting = Formatting.Indented;
xWriter.WriteStartElement("Variables");
xWriter.WriteStartElement("Zombies");
xWriter.WriteValue(Vars.zombies);
xWriter.WriteEndElement();
xWriter.WriteStartElement("Infected");
xWriter.WriteValue(Vars.infected);
xWriter.WriteEndElement();
xWriter.WriteStartElement("Wolfs");
xWriter.WriteValue(Vars.wolfs);
xWriter.WriteEndElement();
xWriter.WriteEndElement();
xWriter.Close();
}
这里我使用WriteValue而不是WriteAttributeString。这给了我们xml像
<?xml version="1.0" encoding="utf-8"?>
<Variables>
<Zombies>1</Zombies>
<Infected>1</Infected>
<Wolfs>1</Wolfs>
</Variables>
现在,我们使用直接值读取来获取数据:
private static void Read()
{
XmlReader xReader = XmlReader.Create("Save1.xml");
while (xReader.Read())
{
if (xReader.NodeType == XmlNodeType.Element)
{
switch (xReader.Name)
{
case "Zombies":
Vars.zombies = xReader.ReadElementContentAsDouble();
break;
case "Infected":
Vars.infected = xReader.ReadElementContentAsDouble();
break;
case "Wolfs":
Vars.wolfs = xReader.ReadElementContentAsDouble();
break;
}
}
}
}
以下是完整演示:https://dotnetfiddle.net/0rQUsD