如果:XElement存在,则Else新建一个
本文关键字:一个 新建 Else XElement 存在 如果 | 更新日期: 2023-09-27 17:59:50
我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<SimpleKD>
<player name="Tardis">
<kills>0</kills>
<deaths>0</deaths>
</player>
</SimpleKD>
首先,我需要检查xml .element("player").attribute("name")
中是否存在用户名"Tardis"。
如果没有,我需要创建一个死亡和死亡为零的游戏。如果是这样,我需要读取死亡和死亡,并将它们设置为变量。
我一直在使用XElement
来尝试这样做。。谢谢
用于编写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)));
假设您有一个Player
类:
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);
如果播放器位于xml的任何部分,则使用simpleKD.Descendants。如果它作为直系子级存在,则使用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) { }
}