2013-11-21 62 views
-1

我在測試中使用了一些代碼幾次,它似乎運作良好。現在我正在爲一款遊戲開發一款助手應用程序,並試圖應用相同的代碼,但它失敗了。我需要知道我是否忽略了一些錯誤。我正在嘗試從xml文件中讀取數據,並在我的winform中填充文本框和組合框。我正確地拉名稱,但其餘屬性不填充。下面是我的代碼示例:無法從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      

      _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文件

<?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加載一個字符,它不在這個例子中工作。我確實在另一個例子中起作用,並且我將兩者緊密地進行了比較,但除了這個例子具有更多不同的屬性之外,找不到任何真正的差異。

注意:我放置了一個斷點,_name值拉動了「柯南」,但所有其他值爲空。

我打算在稍後的某個時候對其進行細化 - 現在就猛擊它。

+0

@Downvoter,抱歉忘了列出我的研究鏈接。 – Nallware

回答

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; 
    } 
} 
+0

感謝您的關注。我知道這會很簡單。我正在複製之前使用過的代碼,這是來自錯誤的方法。 – Nallware

+0

還必須將'code'combobox.SelectedValue = _variable;'code'更改爲'code' combobox.Text = _variable;'code' for all my comboboxes。 – Nallware