I'm making a shortcut program for my daily games I do play. It's sorta like mobile phone folders work just so that you know what I mean.
So far I've come to:
private void addGame(object sender, EventArgs e)
{
string name = string.Empty;
string game_path = string.Empty;
string icon_path = string.Empty;
OpenFileDialog ofdGamePath = new OpenFileDialog();
ofdGamePath.Title = "Choose the game...";
OpenFileDialog ofdIconPath = new OpenFileDialog();
ofdIconPath.Title = "Choose the icon...";
if (ofdGamePath.ShowDialog() == DialogResult.OK)
{
game_path = ofdGamePath.FileName;
}
if (ofdIconPath.ShowDialog() == DialogResult.OK)
{
icon_path = ofdIconPath.FileName;
}
}
Where I currently stuck is the saving to XML path. While on my research I found alot "overkill" solutions which absolutely not fit my scope.
My games.xml looks like the following:
<Games>
<game name="" path="" icon="" />
</Games>
Also reading the file isn't a problem either. Here's the code I use for reading it:
XmlDocument xdoc = new XmlDocument();
xdoc.Load(Application.StartupPath + @"\" + args[1]);
XmlNodeList elemList = xdoc.GetElementsByTagName("game");
for (int i = 0; i < elemList.Count; i++)
{
string name = elemList[i].Attributes["name"].Value;
string game_path = elemList[i].Attributes["path"].Value;
string icon_path = elemList[i].Attributes["icon"].Value;
}
Could anyone direct me into the right direction on how to simply save that one line of XML? Much thanks!
using (var writer = XmlTextWriter.Create("MyFile.xml"))elemList[i].WriteTo(writer);
Okay so I found what I needed!
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath + @"\" + args[1]);
XmlElement el = (XmlElement)doc.SelectSingleNode("Applications");
if (el != null)
{
XmlElement elem = doc.CreateElement("app");
elem.SetAttribute("name", "name online game here");
elem.SetAttribute("path", "123123123");
elem.SetAttribute("icon", "test");
el.AppendChild(elem);
}
doc.Save(Application.StartupPath + @"\" + args[1]);
Resulting in:
<app name="name online game here" path="123123123" icon="test" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.