c# - Saving string with xml -
why if save variables , load them companyname , playername this: system.xml.xmlelement, instead of write? other variables works fine. struggling while, appreciate help, thanks.
public void loadgameprefs() { string filepath = "c:/users/gameprefs.xml"; xmldocument xmldoc = new xmldocument(); if(file.exists (filepath)) { xmldoc.load(filepath); xmlnodelist transformlist = xmldoc.getelementsbytagname("gameprefs"); foreach (xmlnode transforminfo in transformlist) { xmlnodelist transformcontent = transforminfo.childnodes; foreach (xmlnode transformitems in transformcontent) { if(transformitems.name == "firststart") { firststart = bool.parse(transformitems.innertext); } if(transformitems.name == "drawfirstgui") { drawfirststartgui = bool.parse(transformitems.innertext); } if(transformitems.name == "companyname") { companyname = transformitems.innertext; } if(transformitems.name == "playername") { playername = transformitems.innertext; } if(transformitems.name == "money") { scriptmainbackground.money = int.parse(transformitems.innertext); } if(transformitems.name == "year") { year = int.parse(transformitems.innertext); } if(transformitems.name == "month") { month = int.parse(transformitems.innertext); } if(transformitems.name == "week") { week = int.parse(transformitems.innertext); } if(transformitems.name == "day") { day = int.parse(transformitems.innertext); } } } } } public void savegameprefs() { // accesing other script , variable gameobject mainbackground = gameobject.findgameobjectwithtag("mainbackground"); scriptmainbackground scriptmainbackground = mainbackground.getcomponent<scriptmainbackground>(); string filepath = "c:/users/gameprefs.xml"; xmldocument xmldoc = new xmldocument(); if(file.exists (filepath)) { xmldoc.load(filepath); xmlelement elmroot = xmldoc.documentelement; elmroot.removeall(); // remove inside transforms node. xmlelement elmnew = xmldoc.createelement("gameprefs"); xmlelement gameprefs_firststart = xmldoc.createelement("firststart"); gameprefs_firststart.innertext = firststart.tostring(); xmlelement gameprefs_drawfirstgui = xmldoc.createelement("drawfirstgui"); gameprefs_drawfirstgui.innertext = drawfirststartgui.tostring(); xmlelement gameprefs_companyname = xmldoc.createelement("companyname"); gameprefs_companyname.innertext = gameprefs_companyname.tostring(); xmlelement gameprefs_playername = xmldoc.createelement("playername"); gameprefs_playername.innertext = gameprefs_playername.tostring(); xmlelement gameprefs_money = xmldoc.createelement("money"); gameprefs_money.innertext = scriptmainbackground.money.tostring(); xmlelement gameprefs_year = xmldoc.createelement("year"); gameprefs_year.innertext = week.tostring(); xmlelement gameprefs_month = xmldoc.createelement("month"); gameprefs_month.innertext = week.tostring(); xmlelement gameprefs_week = xmldoc.createelement("week"); gameprefs_week.innertext = week.tostring(); xmlelement gameprefs_day = xmldoc.createelement("day"); gameprefs_day.innertext = day.tostring(); //xmlelement gameprefs_genrenumber = xmldoc.createelement("genrenumber"); //gameprefs_day.innertext = genrenumber.tostring(); elmnew.appendchild(gameprefs_firststart); elmnew.appendchild(gameprefs_drawfirstgui); elmnew.appendchild(gameprefs_companyname); elmnew.appendchild(gameprefs_playername); elmnew.appendchild(gameprefs_money); elmnew.appendchild(gameprefs_week); elmnew.appendchild(gameprefs_day); //elmnew.appendchild(gameprefs_genrenumber); elmroot.appendchild(elmnew); xmldoc.save(filepath); // save file. } }
the problem here:
xmlelement gameprefs_playername = xmldoc.createelement("playername"); gameprefs_playername.innertext = gameprefs_playername.tostring();
you setting elements inner text property xmlelement
class + namespace invoking .tostring()
on object.
your intention not clear assume need set innertext
property playername
variable , not string representation of xmlelement
object.
xmlelement gameprefs_companyname = xmldoc.createelement("companyname"); gameprefs_companyname.innertext = companyname; xmlelement gameprefs_playername = xmldoc.createelement("playername"); gameprefs_playername.innertext = playername;
note tostring();
call unesseccary here playername/companyname
variables appear strings judging code in loadgameprefs()
Comments
Post a Comment