因此,当我最初写这篇文章大约6个月前它工作正常,但现在由于某种原因它捕获。我对编码很新,不知道如何缩小编码以找出如何解决这个问题。任何建议将不胜感激。 (它基于一个教程,很少从该教程中更改。添加了一些子节点以保存XML。) 如果我在" playerdata.LoadXml ..."之后在每一行上设置断点在第一个" foreach"之前它会跳过maxhp,maxcarry,trouble和player.CurrentLocation。
public static Player CreatePlayerFromXmlString(string xmlPlayerData)
{
try
{
XmlDocument playerData = new XmlDocument();
playerData.LoadXml(xmlPlayerData);
int currenthp = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentHP").InnerText);
int maxhp = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/MaxHP").InnerText);
int gold = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Gold").InnerText);
int xp = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/XP").InnerText);
int lvl = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Lvl").InnerText);
decimal currentcarry = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentCarry").InnerText);
decimal maxcarry = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/MaxCarry").InnerText);
int trouble = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Trouble").InnerText);
Player player = new Player(currenthp, maxhp, gold, xp, lvl, currentcarry, maxcarry, trouble);
int currentLocationID = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentLocation").InnerText);
player.CurrentLocation = World.LocationByID(currentLocationID);
foreach (XmlNode node in playerData.SelectNodes("/Player/InventoryItems/InventoryItem"))
{
int id = Convert.ToInt32(node.Attributes["ID"].Value);
int quantity = Convert.ToInt32(node.Attributes["Quantity"].Value);
for (int i = 0; i < quantity; i++)
{
player.AddItemToInventory(World.ItemByID(id));
}
}
foreach (XmlNode node in playerData.SelectNodes("/Player/PlayerQuests/PlayerQuest"))
{
int id = Convert.ToInt32(node.Attributes["ID"].Value);
bool isCompleted = Convert.ToBoolean(node.Attributes["IsComplete"].Value);
PlayerQuest playerQuest = new PlayerQuest(World.QuestByID(id));
playerQuest.IsComplete = isCompleted;
player.Quests.Add(playerQuest);
}
return player;
}
catch
{
return Player.CreateDefaultPlayer();
}
}
答案 0 :(得分:1)
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
Console.WriteLine(e.StackTrace);
}
这应该会让你更多地了解正在发生的事情。
答案 1 :(得分:-1)
当您调用try / catch
时,您可以通过指定要捕获的异常来指定要捕获的内容:
try
{
// do stuff...
}
catch ( Exception e1 )
{
// do stuff with e1...
Console.WriteLine(e1.Number + " - " + e1.Message);
}
catch ( AnotherException e2 )
{
// do stuff with e2...
Console.WriteLine(e2.Number + " - " + e2.Message);
}
etc...