how to import xml file in ms access through code

Asked By Reena Jain
05-Feb-10 11:21 PM
Earn up to 0 extra points for answering this tough question.
hello,

I have a xml file , i want to fill my table with this xml data. how to do this by code of c#.net in windows application.

Thanks

  re: how to import xml file in ms access through code

Santhosh N replied to Reena Jain
05-Feb-10 11:38 PM
You could actually load the xml data into the dataset in c#
DataSet ds = new DataSet();
ds.LoadXml(xmlfilepath);

and now, you will be having all the data in the dataset in one datatable..
Using this you can insert into the Access either row by row or if you have any table exactly with same structure you can update the corresponding table..but would suggest first apporach

  re: how to import xml file in ms access through code

Web Star replied to Reena Jain
06-Feb-10 12:42 AM

just use this method for read xml file in datatable in C#.net

  1. public DataTable ReadXML(string file)
  2. {
  3.     //create the DataTable that will hold the data
  4.     DataTable table = new DataTable("XmlData");
  5.     try
  6.     {
  7.         //open the file using a Stream
  8.         using(Stream stream = new  FileStream(file, FileMode.Open, FileAccess.Read))
  9.         {
  10.             //create the table with the appropriate column names
  11.             table.Columns.Add("Name", typeof(string));
  12.             table.Columns.Add("Power", typeof(int));
  13.             table.Columns.Add("Location", typeof(string));
  14.  
  15.             //use ReadXml to read the XML stream
  16.             table.ReadXml(stream);
  17.  
  18.             //return the results
  19.             return table;
  20.         }               
  21.     }
  22.     catch (Exception ex)
  23.     {
  24.         return table;
  25.     }
  26. }

  no i want import option directly from xml to access

Reena Jain replied to Santhosh N
06-Feb-10 01:31 AM
Hello,

I want an import option as avail in access. like there is direct back up and restore command in sql and by code we do it.
because from xml to datatable and then datatable to access . it will slow the process so is there any direct way to import the xml table to access directly.

Thanks for reply
  re: no i want import option directly from xml to access
Santhosh N replied to Reena Jain
06-Feb-10 04:30 AM
Ms Access has an utility to do this without any c# in between this..

Select File-->Get external data --> Import
from ms access and you could specify the xml file to import.

check here for more info on this
  re: re: no i want import option directly from xml to access
Reena Jain replied to Santhosh N
06-Feb-10 04:48 AM
no i have to do it with code only
  re: re: re: no i want import option directly from xml to access
Santhosh N replied to Reena Jain
06-Feb-10 04:49 AM
There is no direct way of doing it in code unlike sql server as far as i know other than parsing the data in the C# code by loading  into the datatable and do as i mentioned...
  re: how to import xml file in ms access through code
Mahipal Reddy replied to Web Star
14-Jun-10 07:55 AM

Hi,

I am trying to compile this programmes in VC#2010.

iam getting lots of errors.

can you pls give little clear information.

my requirement example.

i have 3 xml file in c:\test folder and i want to import xml files to new accessdb.

and i want to include xml file name to access table.

thank you

Create New Account