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

发布于 2024-11-15 21:03:33 字数 917 浏览 1 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忘羡 2024-11-22 21:03:33

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

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"));
}
公布 2024-11-22 21:03:33
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;
}
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;
}
随梦而飞# 2024-11-22 21:03:33
        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);
        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);
末蓝 2024-11-22 21:03:33

如果 Player 位于 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) { }
    }

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) { }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文