Save xml file in c# -


i have program take in name , score, compare score scoreboard located in xml file, , sort fro highest lowest score. works until shutdown program , restarts it, looks has not saved changes xml file.

this xml file:

<?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> 

and code in program:

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 = 1; <= 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 = 0; < 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 = 1; <= 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 = 0; < temp.count; i++)             {                 laboutput.text += (i + 1).tostring() + ". " + temp[i].name + ": " + (temp[i].score).tostring() + "\n";             }         }          struct highscore         {             public int score;             public string name;         }     } } 

i can't find how save after shut down program, comment if there need more information on

i assume form1 work main window of application. in such case should bind callback method application exit event in app.xaml. body of callback method may refer main window using mainwindow property. may cast form1 type , invoke method save results (make public).


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -