This will compile, but I'm thinking that this implementation of read/write is very hard coded and would not be easily adapted to larger situations where large XML documents are being processed or if they are in a different format structure.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
namespace Testing_LINQ_to_XML
{
class Program
{
static string playerName;
static int playerWin = 10;
static int playerLoss = 1;
static int playerTie = 0;
#region Main
static void Main()
{
Console.WriteLine("Enter player Name...");
playerName = Console.ReadLine();
readXML();
writeXML();
readXML();
Exit();
}
#endregion
#region Write
static void writeXML()
{
var path = @"C:\Users\Viper45\Documents\Visual Studio 2013\Projects\Testing LINQ to XML\Testing LINQ to XML\XML Saves\" + playerName + ".xml";
if (System.IO.File.Exists(path)) //Decides if the player has a xml file already
{
//Get data from existing
XDocument file = XDocument.Load(path);
XElement winTemp = new XElement("playerWin", playerWin);
XElement lossTemp = new XElement("playerLoss", playerLoss);
XElement tieTemp = new XElement("playerTie", playerTie);
////delete existing file
File.Delete(Path.Combine(path));
//combine save data with new game
playerWin = playerWin + (int)winTemp;
playerLoss = playerLoss + (int)lossTemp;
playerTie = playerTie + (int)tieTemp;
////Creates new file using last game played.
XDeclaration _obj = new XDeclaration("1.0", "utf-8", "");
XNamespace gameSaves = "gameSaves";
XElement fileNew = new XElement("Root",
new XElement("Player",
new XElement("playerName", playerName),
new XElement("Stats",
new XElement("playerWin", (int)winTemp + playerWin),
new XElement("playerLoss", (int)lossTemp + playerLoss),
new XElement("playerTie", (int)tieTemp + playerTie))));
file.Save(path);
}
else //if the player doesn't have a txt file it creates one here
{
XDeclaration _obj = new XDeclaration("1.0", "utf-8", "");
XNamespace gameSaves = "gameSaves";
XElement file = new XElement("Root",
new XElement("Player",
new XElement("playerName", playerName),
new XElement("Stats",
new XElement ("playerWin", playerWin),
new XElement("playerLoss", playerLoss),
new XElement("playerTie", playerTie))));
file.Save(path);
Console.WriteLine("Save created: " + path);
}
}
#endregion
#region Read
static void readXML()
{
var path = @"C:\Users\Viper45\Documents\Visual Studio 2013\Projects\Testing LINQ to XML\Testing LINQ to XML\XML Saves\" + playerName + ".xml";
if (System.IO.File.Exists(path))
{
XDocument file = XDocument.Load(path);
XElement winTemp = new XElement("playerWin", playerWin);
XElement lossTemp = new XElement("playerLoss", playerLoss);
XElement tieTemp = new XElement("playerTie", playerTie);
Console.WriteLine("\nYour Record Is:\n");
Console.WriteLine("Wins: " + (int)winTemp);
Console.WriteLine("Losses: " + (int)lossTemp);
Console.WriteLine("Ties: " + (int)tieTemp);
}
else
{
Console.WriteLine("\nYou don't have any stats to show yet. Get playing!!!");
}
}
#endregion
#region Exit
static void Exit()
{
Console.WriteLine("Press any key to exit.");
string temp = Console.ReadLine();
}
#endregion
}
}
Here is a sample of what my XML source output looks like:
<Root>
<Player>
<playerName>name</playerName>
<Stats>
<playerWin>10</playerWin>
<playerLoss>1</playerLoss>
<playerTie>0</playerTie>
</Stats>
</Player>
</Root>
Output if compiled:
Enter player Name... name Your Record Is: Wins: 10 Losses: 1 Ties: 0 Your Record Is: Wins: 20 Losses: 2 Ties: 0 Press any key to exit.
I'm looking for input on what I could have done better here, what I could have done to make this more modular for future projects, and what general practices I should be focusing on in the future.
I'm currently trying to learn and focus on a more object oriented approach as I run through these new concepts. Take a look at my GitHub for things I've worked on in the past. My forward plan is to eventually create a static 2D game using VS 2013 & MonoGame. Much further down the road... is to build on that and create a small 2D platformer.