C# - Problem with reading XML more that once. Also error check if exists. [XElement]
<p>Updated with the changes suggested below.</p>

<p>I've been trying to de-bug this for about 2 days now for 3-4 hours on and off and haven't gotten far. Any help is appreciated.</p>

<p>Alright, so essentially this runs every time someone dies. If I run it once, it works fine. However, If I run it a second time it returns nothing.</p>

<pre><code>public static string Death(string username)
    {
        if (File.Exists(path))
        {
            XElement kd = XElement.Load(path);
            Console.WriteLine("[SimplePlugin] Death Recorded for: " + username + "!");
            var player = kd.Elements("player").Single(p =&gt; p.Attribute("name").Value == username);

            int add = int.Parse(player.Element("deaths").Value) + 1; //Dies Here.

            player.SetElementValue("deaths", add);
            player.Save(path);
            kills = int.Parse(player.Element("kills").Value);
            deaths = int.Parse(player.Element("deaths").Value);
            return (username + " has " + kills + " kills and " + deaths + " deaths.");
        }
        else
        {
            Console.WriteLine("[SimpleStats] Writing SimpleConfig.XML...");
            XElement kd = new XElement(
            "SimpleStats",
            new XElement("player",
                new XAttribute("name", username),
            new XElement("kills", 0),
            new XElement("deaths", 0),
            new XElement("time", 0)));
            kd.Save(path);
            Console.WriteLine("[Simple] Done!");
            return (username + " has " + kills + " kills and " + deaths + " deaths.");
        }
    }
</code></pre>

<hr>

<p>My second problem comes when the player name="NAME" in the XML already exists. Even if the username matches, it re-creates it.</p>

<p>Code for this:</p>

<pre><code>public static string Load(string username)
    {

        if (File.Exists(path))
        {
            XElement kd = XElement.Load(path);
            var player = kd.Elements("player").SingleOrDefault(p =&gt; p.Attribute("name").Value == username);
            Console.WriteLine(player);

            if (player == null)
            {
                Console.WriteLine("[SimplePlugin] Writing new XML data for " + username + ".");
                kd.Add(new XElement("player",
                               new XAttribute("name", username),
                           new XElement("kills", 0),
                           new XElement("deaths", 0),
                           new XElement("time", 0)));
                kd.Save(path);
                return (username + " has " + kills + " kills and " + deaths + " deaths.");
            }
            else
            {
                kills = int.Parse(player.Element("kills").Value);
                deaths = int.Parse(player.Element("deaths").Value);
                return (username + " has " + kills + " kills and " + deaths + " deaths.");
            }
        }
        else
        {
            Console.WriteLine("Doesnt Exist");
            Console.WriteLine("[SimpleStats] Writing SimpleConfig.XML...");
            XElement player = new XElement(
            "SimpleStats",
            new XElement("player",
                new XAttribute("name", username),
            new XElement("kills", 0),
            new XElement("deaths", 0),
            new XElement("time", 0)));
            player.Save(path);
            Console.WriteLine("[Simple] Done!");
            return (username + " has " + kills + " kills and " + deaths + " deaths.");
        }
    }
</code></pre>

<p>A string is sent to Load(). Load check to see if the xml exists. If not, creates it. If so, creates it. Then check to see if the username exists in the xml data. If it does it reads it and returns that string. If it doesn't, it creates it.</p>

<p>Also: "This question does not show any research effort; it is unclear or not useful"
How?</p>
