我在测试中使用了一些代码,但它似乎运行良好。现在我正在为游戏开发一个帮助应用程序并试图应用相同的代码,但它失败了。我需要知道是否有一些我忽略的错误。我试图从一个xml文件中读取并在我的winform中填充文本框和组合框。我正确地提取名称,但其余属性不会填充。以下是我的代码示例:
public bool LoadChar(string _filename) //Fetches character stats from xml file and loads to the textboxes
{
try
{
_name = tbName.Text;//hero
XmlDocument doc = new XmlDocument();//hero
doc.Load(_filename);//hero
XmlNode _currNode = doc.SelectSingleNode("/Characters/Char[@name='" + _name + "']");//hero
_currNode.Attributes["st"].Value = _st;
_currNode.Attributes["dx"].Value = _dx;
_currNode.Attributes["iq"].Value = _iq;
_currNode.Attributes["ma"].Value = _ma;
_currNode.Attributes["armor"].Value = _armor;
_currNode.Attributes["hits"].Value = _hits;
_currNode.Attributes["wounds"].Value = _wounds;
_currNode.Attributes["fatigue"].Value = _fatigue;
_currNode.Attributes["attack"].Value = _attack;
_currNode.Attributes["dmgdie"].Value = _dmgdie;
_currNode.Attributes["dmgmod"].Value = _dmgmod;
_currNode.Attributes["exp"].Value = _exp;
_currNode.Attributes["description"].Value = _description;
_currNode.Attributes["equipment_money"].Value = _equipmentmoney;
_currNode.Attributes["weapons_armor"].Value = _wpnarmor;
_currNode.Attributes["talents_spells"].Value = _talentspell;
_currNode.Attributes["gender"].Value = _gender;
_currNode.Attributes["race"].Value = _race;
_currNode.Attributes["type"].Value = _type;
_currNode.Attributes["job"].Value = _job;
tbName.Text = _name;
tbSt.Text = _st;
tbDx.Text = _dx;
tbIq.Text = _iq;
tbMa.Text = _ma;
tbArmor.Text = _armor;
tbHitStop.Text = _hits;
tbWounds.Text = _wounds;
tbFatigue.Text = _fatigue;
tbAttack.Text = _attack;
tbDmgDie.Text = _dmgdie;
tbDmgMod.Text = _dmgmod;
tbExp.Text = _exp;
tbDescription.Text = _description;
tbEquipmentMoney.Text = _equipmentmoney;
tbWeaponArmor.Text = _wpnarmor;
tbTalentSpell.Text = _talentspell;
cbGender.SelectedValue = _gender;
cbRace.SelectedValue = _race;
cbType.SelectedValue = _type;
cbJob.SelectedValue = _job;
return true;
}
catch
{
return false;
}
}
以下是我尝试从中获取数据的xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
- <Characters>
<Char name="Conan" st="15" dx="13" iq="8" ma="10" armor="Leather" hits="2" wounds="2"
fatigue="0" attack="Great Sword" dmgdie="3" dmgmod="+2" exp="69" description="Conan is a
big enthusiastic gentleman" equipment_money="Backpack, 4 gold coins."
weapons_armor="Great Sword, Longbow." talents_spells="Killing, Maiming, Strangling,
Boxing, Florist." gender="Male" race="Human" type="Barbarian" job="Unskilled" />
</Characters>
我通过将我的表单中的文本框和组合框写入xml来创建xml文件,但是现在我想从xml加载一个字符,但是在这个例子中它不起作用。我确实让它在另一个例子中工作,我把两者紧密地比较在一起,但除了这个具有更多不同属性的例子之外,找不到任何真正的差异。
注意:我放置了一个断点,_name值拉“柯南”,但所有其他值都为空。
我打算稍后在某些方面对此进行改进 - 现在就把它搞砸了。
答案 0 :(得分:1)
您没有从XML加载数据。要读取数据,您需要更改以下位置:
public bool LoadChar(string _filename) //Fetches character stats from xml file and loads to the textboxes
{
try
{
_name = tbName.Text;//hero
XmlDocument doc = new XmlDocument();//hero
doc.Load(_filename);//hero
XmlNode _currNode = doc.SelectSingleNode("/Characters/Char[@name='" + _name + "']");//hero
//reading attributes values
_st = _currNode.Attributes["st"].Value;
_dx = _currNode.Attributes["dx"].Value;
_iq = _currNode.Attributes["iq"].Value;
_ma = _currNode.Attributes["ma"].Value;
_armor = _currNode.Attributes["armor"].Value;
_hits = _currNode.Attributes["hits"].Value;
_wounds = _currNode.Attributes["wounds"].Value;
_fatigue = _currNode.Attributes["fatigue"].Value;
_attack = _currNode.Attributes["attack"].Value;
_dmgdie = _currNode.Attributes["dmgdie"].Value;
_dmgmod = _currNode.Attributes["dmgmod"].Value;
_exp = _currNode.Attributes["exp"].Value;
_description = _currNode.Attributes["description"].Value;
_equipmentmoney = _currNode.Attributes["equipment_money"].Value;
_wpnarmor = _currNode.Attributes["weapons_armor"].Value;
_talentspell = _currNode.Attributes["talents_spells"].Value;
_gender = _currNode.Attributes["gender"].Value;
_race = _currNode.Attributes["race"].Value;
_type = _currNode.Attributes["type"].Value;
_job = _currNode.Attributes["job"].Value;
tbName.Text = _name;
tbSt.Text = _st;
tbDx.Text = _dx;
tbIq.Text = _iq;
tbMa.Text = _ma;
tbArmor.Text = _armor;
tbHitStop.Text = _hits;
tbWounds.Text = _wounds;
tbFatigue.Text = _fatigue;
tbAttack.Text = _attack;
tbDmgDie.Text = _dmgdie;
tbDmgMod.Text = _dmgmod;
tbExp.Text = _exp;
tbDescription.Text = _description;
tbEquipmentMoney.Text = _equipmentmoney;
tbWeaponArmor.Text = _wpnarmor;
tbTalentSpell.Text = _talentspell;
cbGender.SelectedValue = _gender;
cbRace.SelectedValue = _race;
cbType.SelectedValue = _type;
cbJob.SelectedValue = _job;
return true;
}
catch
{
return false;
}
}