dataset.writeXML not writing tables

  • Thread starter Thread starter briforge
  • Start date Start date
B

briforge

This is my first shot at this, so please be gentle ;-)

I'm simply trying to save and load data for a game. I'm not even sure
at this point that using XML is the best format. Maybe a flat format
is. But I've read more on using XML so I'm trying that first. Here's
what I've got so far:

DataSet ds = new DataSet("CurrentGame");
DataTable playersTable = ds.Tables.Add("players");
playersTable.Columns.Add("name", Type.GetType("System.String"));
playersTable.Columns.Add("playerType", Type.GetType("System.Int32"));
playersTable.Columns.Add("playerNum", Type.GetType("System.Int32"));
playersTable.Columns.Add("charPiece", Type.GetType("System.Int32"));

DataRow row;
foreach (Player player in players)
{
row = playersTable.NewRow();
row["name"] = player.getName();
row["playerType"] = (int) player.getPlayerType();
row["playerNum"] = (int) player.getPlayerNum();
row["charPiece"] = (int) player.getCharPiece();
}

ds.WriteXml(getFullPath("NewGame.xml"));

The file is being written, but the only output is <CurrentGame/>. What
am I doing wrong?

Thanks
 
You need to add the rows to the collection - at the end of the loop -
row["charPiece"] = (int)player.getCharPiece();
playersTable.Rows.Add(row);
 
Thanks, that did it! It was in the MSDN article I was working from, too,
so I guess I should read those a little better.
 
Back
Top