如果:XElement存在,否则创建新的

时间:2011-06-18 23:27:11

标签: c# find xelement

我的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)));

4 个答案:

答案 0 :(得分:1)

假设您有一个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"));
}

答案 1 :(得分:1)

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;
}

答案 2 :(得分:0)

        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);

答案 3 :(得分:0)

如果播放器位于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) { }
    }