在c#中保存xml文件

时间:2014-06-01 21:43:02

标签: c# xml save

我有一个程序,它将采用名称和分数,它将分数与位于xml文件中的记分板进行比较,然后将其从最高到最低分数进行排序。它一直有效,直到我关闭程序并重新启动它,然后看起来它没有保存对xml文件的更改。

这是xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<Highscore>
  <Data>
    <score number="1" value="250" name="per"/>
    <score number="2" value="200" name="ole"/>
    <score number="3" value="100" name="gunnar"/>
    <score number="4" value="50" name="lars"/>
    <score number="5" value="25" name="bob"/>
  </Data>
</Highscore>

这是程序中的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Xml.Linq;


namespace XML
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            write();
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            SaveHighscore(ChechForBetterScore(LoadHighscore()));

            write();
        }

        private List<highscore> LoadHighscore()
        {
            List<highscore> temp = new List<highscore>();

            var xmlDocument = XDocument.Load("Highscore.xml");

            for(int i = 1; i <= 5; i++)
            {
                var element = xmlDocument.Root
                                .Element("Data")
                                .Elements("score")
                                .FirstOrDefault(x => (string)x.Attribute("number") == (i.ToString()));

                highscore _temp = new highscore();

                _temp.name = element.Attribute("name").Value;
                _temp.score = Convert.ToInt32(element.Attribute("value").Value);

                temp.Add(_temp);
            }

            return temp;
        }

        private List<highscore> ChechForBetterScore(List<highscore> scoreBoard)
        {
            List<highscore> temp = new List<highscore>();

            try
            {
                int userScore = Convert.ToInt32(txtScore.Text);
                string userName = txtName.Text;

                int tempScoreA = userScore;
                int tempScoreB;

                string tempNameA = userName;
                string tempNameB;

                for(int i = 0; i < scoreBoard.Count; i++)
                {
                    highscore _temp = new highscore();

                    if(scoreBoard[i].score < userScore)
                    {
                        tempScoreB = scoreBoard[i].score;
                        tempNameB = scoreBoard[i].name;

                        _temp.score = tempScoreA;
                        _temp.name = tempNameA;

                        tempScoreA = tempScoreB;
                        tempNameA = tempNameB;

                        temp.Add(_temp);
                    }

                    else
                    {
                        _temp.name = scoreBoard[i].name;
                        _temp.score = scoreBoard[i].score;

                        temp.Add(_temp);
                    }
                }

            }
            catch(Exception trown)
            {
                MessageBox.Show(trown.Message, "Error");
            }

            return temp;
        }

        private void SaveHighscore(List<highscore> scoreboard)
        {
            var xmlDocument = XDocument.Load("Highscore.xml");

            for(int i = 1; i <= 5; i++)
            {
                var element = xmlDocument.Root
                                .Element("Data")
                                .Elements("score")
                                .FirstOrDefault(x => (string)x.Attribute("number") == (i.ToString()));

                element.Attribute("name").Value = scoreboard[i - 1].name;
                element.Attribute("value").Value = (scoreboard[i - 1].score).ToString();
            }

            xmlDocument.Save("Highscore.xml");
        }

        private void write()
        {
            labOutput.Text = "";

            List<highscore> temp = LoadHighscore();

            for(int i = 0; i < temp.Count; i++)
            {
                labOutput.Text += (i + 1).ToString() + ". " + temp[i].name + ": " + (temp[i].score).ToString() + "\n";
            }
        }

        struct highscore
        {
            public int score;
            public string name;
        }
    }
}

我无法在关闭程序后找到如何保存它,只需评论是否有需要更多信息的内容

1 个答案:

答案 0 :(得分:0)

我假设Form1将作为您应用程序的主窗口。在这种情况下,您应该将回调方法绑定到App.xaml中的应用程序Exit事件。从该回调方法的主体,您可以使用MainWindow属性引用主窗口。您可以将其强制转换为Form1类型并调用将保存结果的方法(将其公开)。