簡體   English   中英

將XML文件保存在C#中

[英]Save xml file in c#

我有一個程序,它將輸入名稱和分數,它將分數與位於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;
        }
    }
}

關閉程序后,我找不到保存它的方式,只是注釋一下是否有您需要更多信息的信息

我假設Form1將充當您的應用程序的主窗口。 在這種情況下,您應該將回調方法綁定到App.xaml中的應用程序Exit事件。 從該回調方法的正文中,您可以使用MainWindow屬性引用主窗口。 您可以將其轉換為Form1類型並調用將保存結果的方法(將其公開)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM