将 XML 数据加载到 XNA 时出现问题

发布于 2024-10-10 06:36:33 字数 2557 浏览 0 评论 0原文

我正在尝试使用 XML 文件来存储我的高分,这是我的高分班级

[Serializable]
public struct HighScoreData {
    public string[] PlayerName;
    public int[] Score;
    public int[] Level;

    public int Count;

    public HighScoreData(int count) {
        PlayerName = new string[count];
        Score = new int[count];
        Level = new int[count];

        Count = count;
    }
}

,这是我的 XML 文件:

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
<!-- TODO: replace this Asset with your own XML asset data. -->
<Asset Type="Cellular.HighScoreData">
        <PlayerName>
            <Item>1</Item>
            <Item>2</Item>
            <Item>3</Item>
            <Item>4</Item>
            <Item>5</Item>
            <Item>6</Item>
            <Item>7</Item>
            <Item>8</Item>
            <Item>9</Item>
            <Item>10</Item>
        </PlayerName>
        <Score>
            <Item>1</Item>
            <Item>2</Item>
            <Item>3</Item>
            <Item>4</Item>
            <Item>5</Item>
            <Item>6</Item>
            <Item>7</Item>
            <Item>8</Item>
            <Item>9</Item>
            <Item>10</Item>
        </Score>
        <Level>
            <Item>1</Item>
            <Item>2</Item>
            <Item>3</Item>
            <Item>4</Item>
            <Item>5</Item>
            <Item>6</Item>
            <Item>7</Item>
            <Item>8</Item>
            <Item>9</Item>
            <Item>10</Item>
        </Level>
        <Count>10</Count>
</Asset>

XML 加载数据的方法

    public void LoadStoredHighScore() {
        FileStream stream = File.Open(HighScoreFile, FileMode.Open, FileAccess.Read);
        try {
            XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData));
            highScoreList = (HighScoreData) serializer.Deserialize(stream);
        } finally {
            stream.Close();
        }
    }

这是我从“highScoreList = (HighScoreData) serializer.Deserialize(stream);”中的 。这条线, 它给了我异常“XML 文档 (0, 0) 中存在错误。” 内部异常“根元素丢失”。

我做错了什么吗? 任何帮助表示赞赏。

I am trying to use an XML file to store my high score, here is my high score class

[Serializable]
public struct HighScoreData {
    public string[] PlayerName;
    public int[] Score;
    public int[] Level;

    public int Count;

    public HighScoreData(int count) {
        PlayerName = new string[count];
        Score = new int[count];
        Level = new int[count];

        Count = count;
    }
}

And here is my XML file :

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
<!-- TODO: replace this Asset with your own XML asset data. -->
<Asset Type="Cellular.HighScoreData">
        <PlayerName>
            <Item>1</Item>
            <Item>2</Item>
            <Item>3</Item>
            <Item>4</Item>
            <Item>5</Item>
            <Item>6</Item>
            <Item>7</Item>
            <Item>8</Item>
            <Item>9</Item>
            <Item>10</Item>
        </PlayerName>
        <Score>
            <Item>1</Item>
            <Item>2</Item>
            <Item>3</Item>
            <Item>4</Item>
            <Item>5</Item>
            <Item>6</Item>
            <Item>7</Item>
            <Item>8</Item>
            <Item>9</Item>
            <Item>10</Item>
        </Score>
        <Level>
            <Item>1</Item>
            <Item>2</Item>
            <Item>3</Item>
            <Item>4</Item>
            <Item>5</Item>
            <Item>6</Item>
            <Item>7</Item>
            <Item>8</Item>
            <Item>9</Item>
            <Item>10</Item>
        </Level>
        <Count>10</Count>
</Asset>

Here is my method to Load the data from XML

    public void LoadStoredHighScore() {
        FileStream stream = File.Open(HighScoreFile, FileMode.Open, FileAccess.Read);
        try {
            XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData));
            highScoreList = (HighScoreData) serializer.Deserialize(stream);
        } finally {
            stream.Close();
        }
    }

in "highScoreList = (HighScoreData) serializer.Deserialize(stream);" this line,
it gives me exception "There is an error in XML document (0, 0)."
with inner exception "Root element is missing".

Am I doing anything wrong?
Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青芜 2024-10-17 06:36:33

首先,序列化器将查找名为您尝试反序列化的类型的根元素,但它无法找到该根元素。

接下来,您无法反序列化到此结构,因为它没有默认构造函数。序列化器无法确定要传递什么作为计数参数。阅读 这篇文章以获取更多详细信息。

这是建议的根结构:

<?xml version="1.0" encoding="utf-8" ?>
<HighScoreData>
    <PlayerName>
    ...
</HighScoreData>

First off, the serializer will look for a root element named as the type you are trying to deserialize, which it cannot find.

Next, you cannot deserialize into this structure since it have no default constructor. The serializer have no way of figuring out what to pass as the count parameter. Read up on this article to get further details.

Here's a suggested root structure:

<?xml version="1.0" encoding="utf-8" ?>
<HighScoreData>
    <PlayerName>
    ...
</HighScoreData>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文