简体   繁体   English

如何在基本游戏中获取C#代码以读取XML文件

[英]how to get C# code to read XML file in a basic game

I am creating a basic text based rpg and am trying to use xml to save games. 我正在创建基于RPG的基本文本,并尝试使用xml来保存游戏。 This is the code that calls the players data from an xml: 这是从xml调用玩家数据的代码:

public class SuperAdventure() {
    private Player _player;

    private Monster _currentMonster;

    private const string PLAYER_DATA_FILE_NAME = "PlayerData.xml";

    public SuperAdventure()
    {
        InitializeComponent();

        if (File.Exists(PLAYER_DATA_FILE_NAME))
        {
            _player = Player.CreatePlayerFromXmlString(File.ReadAllText(PLAYER_DATA_FILE_NAME));
        }
        else
        {
            _player = Player.CreateDefaultPlayer();
        }

        MoveTo(_player.CurrentLocation);
        UpdatePlayerStats();
    }
}

I also know for sure that the xml is being created correctly. 我也知道可以正确创建xml。 When I close the game, I can manually find the xml. 当我关闭游戏时,可以手动找到xml。 All the stats are saved but when the game is reopened the player is reset to the default stats and items. 保存所有统计信息,但是当重新打开游戏时,玩家将重置为默认统计信息和项目。 I'm not sure if the order of the function calling would matter or if the creation of the xml would affect the games use of the xml. 我不确定函数调用的顺序是否重要,或者xml的创建是否会影响xml的游戏使用。

Here's the CreatePlayerFromXmlString() function: 这是CreatePlayerFromXmlString()函数:

        public static Player CreatePlayerFromXmlString(string xmlPlayerData)
    {
        try
        {
            XmlDocument playerData = new XmlDocument();

            playerData.LoadXml(xmlPlayerData);

            int currentHitPoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentHItPoints").InnerText);
            int maximumHitPoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/MaximumHitPoints").InnerText);
            int gold = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Gold").InnerText);
            int experiencePoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/ExperiencePoints").InnerText);

            Player player = new Player(currentHitPoints, maximumHitPoints, gold, experiencePoints);

            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["IsCompleted"].Value);

                PlayerQuest playerQuest = new PlayerQuest(World.QuestByID(id));
                playerQuest.IsCompleted = isCompleted;

                player.Quests.Add(playerQuest);
            }

            return player;
        }
        catch
        {
            //If there was an error with the XML data, return a default player object
            return Player.CreateDefaultPlayer();
        }
    }

The problem is when the game went through the try block of code there was an error. 问题是当游戏经过try代码块时出现错误。 This caused it to move to the catch block and create a default player instead. 这导致它移动到catch块并创建一个默认播放器。 The string in the line of code searching for the currentHitPoints is misspelled-visual studio didn't see it as an error because it was a string passed into an XML function. 搜索currentHitPoints的代码行中的字符串是拼写错误的,Visual Studio不会将其视为错误,因为它是传递给XML函数的字符串。

int currentHitPoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentHItPoints").InnerText);

The "/Player/Stats/CurrentHItPoints" should be "/Player/Stats/CurrentHitPoints" "/Player/Stats/CurrentHItPoints"应为"/Player/Stats/CurrentHitPoints"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM