By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,557 Members | 1,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,557 IT Pros & Developers. It's quick & easy.

XML file to MS Access using C#

P: 4
I have created xml files in C# using the DataSet ds.WriteXml method.

I now need to take an xml file and be able to do an insert or delete to a table in MS Access.
How would I go about that?

Do I have to load the XML into a DataSet and then Create command parameters based on the records in the Dataset or is there a simpler way to do this?

Jan 7 '09 #1
Share this Question
Share on Google+
4 Replies

Expert 100+
P: 190
As far as writing Xml to a file and then reading it back into a dataset, that should be straightforward. Here are two links:
DataSet.ReadXml(string fileName)
CodeProject: Convert XML Data to DataSet and Back.

You ask:
Do I have to ... Create command parameters based on the records in the Dataset
Well, the Insert and Delete parameters would be based on the fields. However, if you use a DataAdapter and call the DataAdapter.Update method IDataAdapter.Update Method (System.Data) to send any changes back to the database, then you probably can base this on the RowState of the rows you want to update or delete. However, you would have to load the original rows, then delete or insert the new rows within the DataSet, then call AcceptChanges(), and then DataAdapter.Update().

Yes, you will have to set the Update and InsertCommand text or stored procedure. You can try using CommandBuilder.GetUpdate/InsertCommand method, but if you ever look at what it actually produces, it is very verbose.

I have done the following to update/insert from a dataSet based off xml:
1. In SqlServer, you can actually send the XmlDocument as a stored procedure parameter...that is cool...but you are using Access.
2. Fill a DataSet and use the CommandBuilder to generate the Insert/Update/Delete Commands.
3. Write my own sql text command or stored procedures which basically take only the needed fields to do the update or delete. The Command Parameter(s) can be hooked up to one or more DataTable columns, and the DataSet can be updated in such a way that the command/stored procedure is run against each row.
Jan 8 '09 #2

P: 4
Thanks much..
The thing I was missing was I needed to use the CommandBuilder..

The only issue I have right now is I can not get the delete to work. According to MS I am suppose to use the Delete() method on the record to flag it for delete so when I do an Update it will use the DeleteCommand. However when I do that it Deletes the records and does not flag them.
The RowState is a ReadOnly property so I cant just set that..

Here is my code..
Expand|Select|Wrap|Line Numbers
  1. IDbCommand cmd = CreateCommand("SELECT * FROM VehicleFPM", CommandType.Text);
  2. DataSet ds = new DataSet();
  3. //Read XML file into dataset
  4. ds.ReadXml(xmlFile, XmlReadMode.Auto);
  6. //Mark the records so they will be deleted
  7. int rows = ds.Tables[0].Rows.Count;
  8. for (int i = 0; i < rows; i++)
  9. //Does not flag the record for delete - It Deletes them!!
  10. ds.Tables[0].Rows[0].Delete();
  12. OleDbDataAdapter testadp = new OleDbDataAdapter();
  14. testadp.SelectCommand = (OleDbCommand) cmd;
  15. OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(testadp);
  16. //Put brackets around Field Names incase they have spaces in the names.
  17. cmdBuilder.QuotePrefix = "[";
  18. cmdBuilder.QuoteSuffix = "]";
  20. //Auto generate the Delete and Insert Command. This is done based
  21. testadp.DeleteCommand = cmdBuilder.GetDeleteCommand();
  22. testadp.InsertCommand = cmdBuilder.GetInsertCommand();
  24. recordsAffected = testadp.Update(ds, "VehicleFPM");
  25. ds.AcceptChanges();
If you have any ideas on how to flag the recods for delete.. That would be a help..

Jan 9 '09 #3

Expert 100+
P: 190
First, I was wrong in saying you need to execute "AcceptChanges()" before "Update()". You did not do this, so you code is basically correct. Doing so would have reset the RowState properties.

That said, and apologies if this is might need to call AcceptChanges after loading the Xml document, but before deleting the rows.
I am not positive, but I have had similar difficulties myself and often is is simply resolved by tinkering with the order of statements.

Also, the DataTable maintains both the original version of the rows and the changes version. So even if it appears that the rows are simply deleted and not there anymore, there is a DataRowVersion property associated with the before and after state of the row.

Or it might be an issue of command statement order. Check out this snippet from MSDN:
Expand|Select|Wrap|Line Numbers
  1. DataTable table = dataSet.Tables["Customers"];
  3. // First process deletes.
  4. adapter.Update(table.Select(null, null, DataViewRowState.Deleted));
  6. // Next process updates.
  7. adapter.Update(table.Select(null, null, 
  8.   DataViewRowState.ModifiedCurrent));
  10. // Finally, process inserts.
  11. adapter.Update(table.Select(null, null, DataViewRowState.Added));
Here is another useful link to see if you can resolve the problem:
Jan 9 '09 #4

P: 4
That was it..
When I pulled in the XML into the dataset the records are marked as Added. This is great for an insert.

However, since the records are already marked as Added in order to now mark them as Deleted you must first AcceptChanges so they are now marked as Unchanged then the Delete() will mark them as Deleted.

Why it actually Deletes the records if not in the Unchanged RowState seems a bit odd to me.. But what ever..

Thanks much for the information..
Sometimes just a set of second eyes really helps alot..

Here is the Code incase anyone else runs into the issue...
Expand|Select|Wrap|Line Numbers
  1. DataSet ds = new DataSet();
  2. //Determine if the xml file has schema. If so then use it.
  3. //If not the infer it from the file.
  4. ds.ReadXml(xmlFile, XmlReadMode.Auto);
  5. string tablename = ds.Tables[0].TableName;
  6. IDbCommand cmd = CreateCommand("SELECT * FROM " + tablename, CommandType.Text);
  7. if (procedureType == UpdateType.Delete)
  8.  {
  9.       //Records are currently marked as Added.
  10.       //Must AcceptChanges so the records will be marked as Deleted
  11.       ds.AcceptChanges();
  12.        //Mark the records so they will be deleted
  13.        foreach (DataRow dr in ds.Tables[0].Rows)
  14.        {
  15.               dr.Delete();
  16.         }
  17.  }
  19. IDbDataAdapter dataAdpt = CreateDataAdapter(cmd);
  21. dataAdpt.SelectCommand = (OleDbCommand) cmd;
  22. OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder((OleDbDataAdapter) dataAdpt);
  23.  //Put brackets around Field Names incase they have spaces in the names.
  24.  cmdBuilder.QuotePrefix = "[";
  25.  cmdBuilder.QuoteSuffix = "]";
  26.   //Auto generate the Delete and Insert Command. This is done based
  27.  dataAdpt.DeleteCommand = cmdBuilder.GetDeleteCommand();
  28.  dataAdpt.InsertCommand = cmdBuilder.GetInsertCommand();
  29.  dataAdpt.TableMappings.Add("Table", tablename);
  30.  recordsAffected = dataAdpt.Update(ds);
  32. ds.AcceptChanges();
Jan 9 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.