简体   繁体   English

序列化xml文件中的数据时出错

[英]Error serializing data in an xml file

I have a problem with the highscore code for a game I am creating in school. 我在学校创建的游戏的highscore代码有问题。

When the game ends it says that it cant access HighScore because of its protection level. 游戏结束时,它说,由于其保护级别,它无法访问HighScore

This is the code that is giving me an error : 这是给我一个错误的代码:

public static HighScore LoadHighScores(string filename)
{
        HighScore data;
        // Get the path of the save game
        string fullpath = "highscores.xml";

        // Open the file
        FileStream stream = File.Open(fullpath, FileMode.OpenOrCreate, FileAccess.Read);
        try
        {
            // Read the data from the file
            XmlSerializer serializer = new XmlSerializer(typeof(HighScore));
            data = (HighScore)serializer.Deserialize(stream);
        }
        finally
        {
            // Close the file
            stream.Close();
        }


        return (data);
}

When i run the code and die i call this code that saves the score i got: 当我运行代码并死亡时,我将此代码称为保存得分的代码:

public void SaveHighScore(int score)
{
        // Create the data to saved
        HighScore data = LoadHighScores(HighScoresFilename);
        int scoreIndex = -1;
        for (int i = data.Count--; i > -1; i--)
        {
            if (score >= data.Score[i]) //score.getScore();
            {
                scoreIndex = i;
            }
        }

        if (scoreIndex > -1)
        {
            //New high score found ... do swaps
            for (int i = data.Count--; i > scoreIndex; i--)
            {
                data.PlayerNames[i] = data.PlayerNames[i - 1];
                data.Score[i] = data.Score[i - 1];
            }

            data.PlayerNames[scoreIndex] = PlayerName; //Retrieve User Name Here
            data.Score[scoreIndex] = score; // Retrieve score here

            SaveHighScores(data, HighScoresFilename);
        }
}

And SaveHighScores looks like this: SaveHighScores看起来像这样:

public static void SaveHighScores(HighScore data, string filename)
{
        // Get the path of the save game
        string fullpath = "highscores.xml";

        // Open the file, creating it if necessary
        FileStream stream = File.Open(fullpath, FileMode.OpenOrCreate);
        try
        {
            // Convert the object to XML data and put it in the stream
            XmlSerializer serializer = new XmlSerializer(typeof(HighScore));
            serializer.Serialize(stream, data);
        }
        finally
        {
            // Close the file
            stream.Close();
        }
}

Please check this link Public Class - "is inaccessible due to its protection level. Only public types can be processed." 请检查此链接公共类-“由于其保护级别而无法访问。只能处理公共类型。”

you can find your answer there.... 您可以在那里找到答案。...

As a summary mark your HighScore as public. 作为摘要,将您的HighScore标记为公开。

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

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