c# - How do I load multiple XML files and read them? -
i've problem. in code i'm looking xml files in 1 directory. works don't know how read them. single 1 loaded string xmlfile. think need replace or edit in way code xmlfile not 1 file, files found in directory.
what should doing?
namespace windowsformsapplication11 { public partial class form1 : form { private const string xmlfile = "c:\games\games.xml"; // single xml file works public form1() { this.initializecomponent(); this.initializelistview(); this.loaddatafromxml(); listview.items.addrange(directory.getfiles("c:\games\", "*.xml") .select(f => new listviewitem(f)) .toarray()); } private void loaddatafromxml() { if (file.exists(xmlfile)) { xdocument document = xdocument.load(xmlfile); if (document.root != null) { foreach (xelement gameelement in document.root.elements("game")) { string gamename = gameelement.element("gamename").value; string launchpath = gameelement.element("launchpath").value; string uninstallpath = gameelement.element("uninstallpath").value; string publisher = gameelement.element("publisher").value; // check if gameelement.element(elementname) not null game game = new game(gamename, launchpath, uninstallpath, publisher); addgametolistview(game); } } } } private void addgametolistview(game game) { listviewitem item = creategamelistviewitem(game); this.listview.items.add(item); } private listviewitem creategamelistviewitem(game game) { listviewitem item = new listviewitem(game.gamename); item.subitems.add(game.launchpath); item.subitems.add(game.uninstallpath); item.subitems.add(game.publisher); item.tag = game; return item; } private void initializelistview() { this.listview.view = view.details; this.listview.gridlines = true; this.listview.multiselect = false; this.listview.fullrowselect = true; this.listview.columns.addrange(new[] { new columnheader{text = "gamename", width = 200}, new columnheader{text = "launchpath"}, new columnheader{text = "uninstallpath"}, new columnheader{text = "publisher"} }); this.listview.mousedoubleclick += listviewonmousedoubleclick; } private void listviewonmousedoubleclick(object sender, mouseeventargs e) { if (e.button == mousebuttons.left && this.listview.selecteditems.count > 0) { game game = (game)((this.listview.selecteditems[0].tag); try { process.start(game.launchpath); } catch (exception ex) { messagebox.show("can not start game.\ndetails:\n" + ex.message, "error", messageboxbuttons.ok, messageboxicon.error); } } } } internal class game { public game() { } public game(string gamename, string launchpath, string uninstallpath, string publisher) { this.gamename = gamename; this.launchpath = launchpath; this.uninstallpath = uninstallpath; this.publisher = publisher; } public string gamename { get; set; } public string launchpath { get; set; } public string uninstallpath { get; set; } public string publisher { get; set; } } }
update:
this current code. how can send f loaddatafromxml ?
public form1() { this.initializecomponent(); this.initializelistview(); this.height = screen.primaryscreen.workingarea.height; var files = directory.getfiles(@"c:\games\", "*.xml").select(f => new listviewitem(f)).toarray(); listview.items.addrange(files); foreach (var f in files) { this.loaddatafromxml(f); } } private void loaddatafromxml(//what need enter here?) { foreach (xelement gameelement in f.root.elements("game")) { string gamename = gameelement.element("gamename").value; string launchpath = gameelement.element("launchpath").value; string portablesave = gameelement.element("portablesave").value; string publisher = gameelement.element("publisher").value; string gameid = gameelement.element("gameid").value; string update = gameelement.element("update").value; // check if gameelement.element(elementname) not null game game = new game(gamename, launchpath, portablesave, publisher, gameid, update); addgametolistview(game); } }
simple use directory functions xml files, , loop through them, calling loaddatafromxml
each file. note: need refactor code little bit.
you need modify loaddatafromxml
take file
parameter. , change form1 constructor this
public form1() { this.initializecomponent(); this.initializelistview(); var files = directory.getfiles("c:\games\", "*.xml") .select(f => new listviewitem(f)) .toarray(); listview.items.addrange(files); foreach(var f in files) { this.loaddatafromxml(f); } }
Comments
Post a Comment