简体   繁体   中英

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. This is the code that calls the players data from an 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. When I close the game, I can manually find the 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.

Here's the CreatePlayerFromXmlString() function:

        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. This caused it to move to the catch block and create a default player instead. 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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