How to update specific values in XML document with C# -
i asked similar question had problems proposed solution, rephrasing differently sample code time. have xml file stores data players of game. need find player periodically xml file , update associated data specific player plays game:
<?xml version="1.0"?> <playerstats xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> <player> <name>jack</name> <wincount>3</wincount> <playcount>9</playcount> <balance>500</balance> </player> <player> <name>john</name> <wincount>3</wincount> <playcount>9</playcount> <balance>940</balance> </player> </playerstats>
i need code identify specific player (example, john) , update wincount, playcount, , balance numbers based on c# variables. here sample c# code working with:
xmldocument doc = new xmldocument(); doc.load(xmlfilepath); xmlnode player; xmlnode root = doc.documentelement; // below correctly pulls data specific player player = root.selectsinglenode("descendant::player[name='"+form1.strplayername+"']"); // "inner xml" player = "<name>john</name><wincount>3</wincount><playcount>9</playcount><balance>940</balance>" // since "balance" last, tried using "lastchild" , code below worked player.lastchild.innertext = form1.decbalance.tostring(); //updates balance succesfully doc.save(xmlfilepath);
so works "lastchild", how change "wincount", "playcount", , "balance" without referencing them either first or last? got suggestions before using linq , xml serialization, etc., caused problems , not understand linq yet. want use xmldocument bc feel code 95% of way there , missing easy. new c# using of code above make life easier if possible. thanks,
i believe can this:
player["wincount"].innertext = form1.wincount.tostring(); player["playcount"].innertext = form1.playcount.tostring(); player["balance"].innertext = form1.decbalance.tostring();
documentation here.
Comments
Post a Comment