public static void ImportPlayerXml(string XmlPath)
{
// Connect to database first!
using (StatsDatabase Driver = new StatsDatabase())
{
// Load elements
XDocument Doc = XDocument.Load(new FileStream(XmlPath, FileMode.Open, FileAccess.Read));
XElement Info = Doc.Root.Element("Info");
XElement TableData = Doc.Root.Element("TableData");
// Make sure player doesnt already exist
int Pid = Int32.Parse(Info.Element("Pid").Value);
if (Driver.PlayerExists(Pid))
throw new Exception(String.Format("Player with PID {0} already exists!", Pid));
// Begin Transaction
using (DbTransaction Transaction = Driver.BeginTransaction())
{
try
{
// Loop through tables
foreach (XElement Table in TableData.Elements())
{
// Loop through Rows
foreach (XElement Row in Table.Elements())
{
InsertQueryBuilder QueryBuilder = new InsertQueryBuilder(Table.Name.LocalName, Driver);
foreach (XElement Col in Row.Elements())
{
if (Col.Name.LocalName == "name")
QueryBuilder.SetField(Col.Name.LocalName, Col.Value.UnescapeXML());
else
QueryBuilder.SetField(Col.Name.LocalName, Col.Value);
}
QueryBuilder.Execute();
}
}
// Commit Transaction
Transaction.Commit();
}
catch
{
Transaction.Rollback();
throw;
}
}
}
}