My XML File:
<?xml version="1.0" encoding="utf-8"?>
<SimpleKD>
<player name="Tardis">
<kills>0</kills>
<deaths>0</deaths>
</player>
</SimpleKD>
First off, I need to check if the username "Tardis" exists in the xml .element("player").attribute("name")
.
If it doesn't, i need to create it with kills and deaths at zero. If it does, I need to read kills and deaths and set them to variables.
I have been using XElement
to try and do this.. Thanks!
Code used to write the XML:
public static string username = "Tardis";
public static int kills = 0;
public static int deaths = 0;
........
XElement Players = new XElement(
"SimpleKD",
new XElement("player",
new XAttribute("name", username),
new XElement("kills", kills),
new XElement("deaths", deaths)));
Assuming you have a Player
class:
private static Player GetPlayer(string name, XElement simpleKD)
{
var playerElem = simpleKD.Elements("player")
.SingleOrDefault(p => p.Attribute("name").Value == name);
if (playerElem == null)
{
simpleKD.Add(new XElement("player",
new XAttribute("name", name),
new XElement("kills", 0),
new XElement("deaths", 0)));
return new Player(name);
}
return new Player(name,
(int)playerElem.Element("kills"),
(int)playerElem.Element("deaths"));
}
XmlDocument xmlfile= new XmlDocument();
xmlfile.Load("FileName.xml");
XmlNode node = xmlfile.SelectSingleNode("//SimpleKD");
XmlElement element = (XmlElement)node.SelectSingleNode(string.Format("//player[@name='{0}']", "Tardis"));
if(element == null)
{
element = node.CreateElement("player");
element .SetAttribute("name", "Tardis");
node.AppendChild(element);
xmlfile.Save("FileName.xml");
XmlElement newElement = new Element();
newElement = node.CreateElement("Kills");
newElement.Value = 0;
element.AppendChild(newElement);
newElement = node.CreateElement("Deaths");
newElement.Value = 0;
element.AppendChild(newElement);
xmlfile.Save("FileName.xml");
}
else
{
kills = element.FirstChild.Value;
deaths = element.LastChild.Value;
}
int k = 0, d = 0; //kills and deaths
var doc = XDocument.Parse(xmlString);
var elem = doc.XPathSelectElement("/SimpleKD/player[@name='Tardis']");
if (elem != null)
{
var killsNode = (XElement)elem.FirstNode;
k = (int)killsNode;
var deathsNode = (XElement)killsNode.NextNode;
d = (int)deathsNode;
}
else
{
elem = doc.XPathSelectElement("/SimpleKD");
elem.Add(new XElement("player",
new XAttribute("name", "Tardis"),
new XElement("kills", k),
new XElement("deaths", d)));
}
xmlString = doc.ToString();
Console.WriteLine("Kills: {0}", k);
Console.WriteLine("Deaths: {0}", d);
If the Player is in any part of xml then use simpleKD.Descendants. If it exists as immediate children then use simpleKD.Elements
private Player GetPlayer(string name, XElement simpleKD)
{
var playerElem = simpleKD.Descendants("player").SingleOrDefault(x => x.Attribute("name").Value.Equals(name));
if (playerElem == null)
{
simpleKD.Add(new XElement("player",
new XAttribute("name", name),
new XElement("kills", 0),
new XElement("deaths", 0)));
return new Player(name);
}
return new Player(name,
(int)playerElem.Element("kills"),
(int)playerElem.Element("deaths"));
}
class Player
{
public string Kills { get; set; }
public string Deaths { get; set; }
public Player() { }
public Player(string name) { }
public Player(string name, int kills, int deaths) { }
}
精彩评论