Reputation: 814
I'm trying to figure out how to store data using C#.
I'm new to C# data storage, having used PHP and MySQL to do this kind of thing in the past. After doing some research, it seems that using XML serialization is the way to go but I'm not quite sure of all the details.
So far, I've created my table with several columns:
private void SerializeDataSet()
{
XmlSerializer ser = new XmlSerializer(typeof(DataSet));
// Creates a DataSet for user info
DataSet vault = new DataSet("vault");
DataTable table_users = new DataTable("users");
DataColumn last_name = new DataColumn("last_name");
DataColumn first_name = new DataColumn("first_name");
DataColumn birthdate = new DataColumn("birthdate");
DataColumn role = new DataColumn("role");
table_users.Columns.Add(last_name);
table_users.Columns.Add(first_name);
table_users.Columns.Add(birthdate);
table_users.Columns.Add(role);
// adds table
vault.Tables.Add(table_users);
// serialized table
string filename = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Resources) + "Vault_Tec_storage";
TextWriter writer = new StreamWriter(filename);
ser.Serialize(writer, vault);
writer.Close();
}
Now what I can't figure out how to do, is actually add the data to that table. For example, if I wanted a method to process a user registration for John Doe, born 10/15/1975 with role "0," how would I go about doing that?
Upvotes: 0
Views: 2031
Reputation: 894
you might want to try the DataSet ReadXml()
Usage is something like this:
using (StreamReader reader = new StreamReader("C://filepath.xml"))
{
byte[] fullByte = Encoding.ASCII.GetBytes(reader.ReadToEnd());
DataSet dataSet = new DataSet();
MemoryStream mStream = new MemoryStream(fullByte);
mStream.Position = 0;
dataSet.ReadXml(mStream);
/*
while DEBUGGING, you'll notice here that your `dataSet` is now populated by
collection of tables. These table came for each sub nodes followed with an
internal ID as relationship between nodes. From here, you may add values in
your table like the usage below.
*/
// ex. if Table is like this
// Name | Age
// John | 09/10/2015
DataTable dt = dataSet.Tables[0];
dt.Rows.Add(
// array of objects
new object[] { "New John", "09/11/2015" }
);
//OR
DataRow dr = dt.NewRow();
dr["Name"] = "New John";
dr["Age"] = "09/11/2015";
dt.Rows.Add(dr);
}
hopefully this helps, if you have a specific case you're struggling at, kindly line it under the comment section, we'll help you out.
Thanks!
Upvotes: 2
Reputation: 34421
Try this
private void SerializeDataSet()
{
// Creates a DataSet for user info
DataSet vault = new DataSet("vault");
DataTable table_users = new DataTable("users");
table_users.Columns.Add("last_name", typeof(string));
table_users.Columns.Add("first_name", typeof(string));
table_users.Columns.Add("birthdate", typeof(DateTime));
table_users.Columns.Add("role", typeof(int));
// adds table
vault.Tables.Add(table_users);
table_users.Rows.Add(new object[] { "Doe", "John", DateTime.Parse("10/15/1975"), 0 });
// serialized table
string filename = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Resources) + "Vault_Tec_storage";
vault.WriteXml(filename);
}
Upvotes: 1