所以我正在做一个项目,在从文本中获取一些元素后,用XML替换某些元素时遇到了问题
我尝试了很多选项,先删除再添加 XML是这样的:
<?xml version="1.0" encoding="utf-8"?>
<UserAccountData>
<user>
<username>Admin</username>
<password>Partyguest12</password>
<ID1>1<stats level="50" HP="1000" exp="250000" /></ID1>
</user>
<user>
<username>2</username>
<password>2</password>
<ID2>2<stats Level="1" HP="20" exp="0" /></ID2>
</user>
<user>
<username>3</username>
<password>3</password>
<ID3>3<stats Level="1" HP="20" exp="0" /></ID3>
</user>
<user>
<username>4</username>
<password>4</password>
<ID4>4<stats Level="1" HP="20" exp="0" /></ID4>
</user>
<user>
<username>5</username>
<password>5</password>
<ID5>5<stats Level="1" HP="20" exp="0" /></ID5>
</user>
</UserAccountData>
public partial class Game : MetroFramework.Forms.MetroForm
{
private void Save_data_Progress_Click(object sender, EventArgs e)
{
int IDcheck = 0;
XmlDocument User_Data_Exp_Use = new XmlDocument();
User_Data_Exp_Use.Load("UserData.xml");
foreach (XmlNode ID_Finder in User_Data_Exp_Use.SelectNodes("UserAccountData/user"))
{
IDcheck++;
if ((Account.Text == ID_Finder.SelectSingleNode("username").InnerText) && (Log_In_Data.PassKey == ID_Finder.SelectSingleNode("password").InnerText))
{
break;
}
}
XmlNode User_Path = User_Data_Exp_Use.SelectSingleNode("UserAccountData/user/ID" + IDcheck);
XmlElement Stats_Set = User_Data_Exp_Use.CreateElement("stats");
Stats_Set.SetAttribute("Level", Lv.Text);
Stats_Set.SetAttribute("HP", Hit_Points.Text);
Stats_Set.SetAttribute("exp",XP.Text);
User_Path.AppendChild(Stats_Set);
User_Data_Exp_Use.Save("UserData.xml");
}
}
运行后,我什么也没得到,也没有得到任何XML错误,说它为NULL