473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

XML file to MS Access using C#

codemama
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?

Thanks
Jan 7 '09 #1
4 7319
mldisibio
190 Expert 100+
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)
and
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
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);
  5.  
  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();
  11.  
  12. OleDbDataAdapter testadp = new OleDbDataAdapter();
  13.  
  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 = "]";
  19.  
  20. //Auto generate the Delete and Insert Command. This is done based
  21. testadp.DeleteCommand = cmdBuilder.GetDeleteCommand();
  22. testadp.InsertCommand = cmdBuilder.GetInsertCommand();
  23.  
  24. recordsAffected = testadp.Update(ds, "VehicleFPM");
  25. ds.AcceptChanges();
  26.  
If you have any ideas on how to flag the recods for delete.. That would be a help..

Thanks
Jan 9 '09 #3
mldisibio
190 Expert 100+
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 confusing...you 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 http://msdn.microsoft.com/en-us/library/system.data.datarowversion(VS.80).aspx 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"];
  2.  
  3. // First process deletes.
  4. adapter.Update(table.Select(null, null, DataViewRowState.Deleted));
  5.  
  6. // Next process updates.
  7. adapter.Update(table.Select(null, null, 
  8.   DataViewRowState.ModifiedCurrent));
  9.  
  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:
http://msdn.microsoft.com/en-us/library/33y2221y(VS.80).aspx
Jan 9 '09 #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.  }
  18.  
  19. IDbDataAdapter dataAdpt = CreateDataAdapter(cmd);
  20.  
  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);
  31.  
  32. ds.AcceptChanges();
  33.  
Jan 9 '09 #5

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

Similar topics

5
by: | last post by:
I am having a major problem with file transfers - they are ending early when the bandwidth tops-out. Smaller files transfer just fine, but large files (12Mb+) can 'abort' the transfer after maybe...
18
by: Dino | last post by:
dear all, i've created an application for a customer where the customer can upload ..csv-files into a specified ftp-directory. on the server, a php-script, triggered by a cronjob, reads all the...
0
by: Fran Tirimo | last post by:
I am developing a small website using ASP scripts to format data retrieved from an Access database. It will run on a Windows 2003 server supporting FrontPage extensions 2002 hosted by the company...
2
by: Fran Tirimo | last post by:
I am developing a small website using ASP scripts to format data retrieved from an Access database. It will run on a Windows 2003 server supporting FrontPage extensions 2002 hosted by the company...
23
by: Lamberti Fabrizio | last post by:
Hi all, I've to access to a network file from an asp pages. I've red a lot of things on old posts and on Microsoft article but I can't still solve my problem. I've got two server inside the...
9
by: John | last post by:
Hi All, I have encountered a problem with the ldb file of a database. When making changes to the backend databases I need exclusive access to make changes. I usually do this at night when all...
13
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it...
9
by: JimmyKoolPantz | last post by:
IDE: Visual Studio 2005 Language: VB.NET Fox Pro Driver Version: 9.0.0.3504 Problem: I currently have a problem altering a DBF file. I do not get any syntax errors when running the program. ...
5
by: =?Utf-8?B?QWRyaWFuTW9ycmlz?= | last post by:
Hello! I'm trying to copy a file from another computer on the network that I do not have permission with my current logon details to access. If I open the folder using the Windows file manager...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.