C# .NET - how to import xml file in ms access through code

Asked By Reena Jain
05-Feb-10 11:21 PM
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
  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
  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 = http://www.google.com/search?q=new+msdn.microsoft.com DataTable("XmlData");
  5.     try
  6.     {
  7.         //open the file using a Stream
  8.         using(Stream stream = http://www.google.com/search?q=new+msdn.microsoft.com  FileStream(file, FileMode.Open, FileAccess.Read))
  9.         {
  10.             //create the table with the appropriate column names
  11.             table.Columns.Add("Name", http://www.google.com/search?q=typeof+msdn.microsoft.com(string));
  12.             table.Columns.Add("Power", http://www.google.com/search?q=typeof+msdn.microsoft.com(int));
  13.             table.Columns.Add("Location", http://www.google.com/search?q=typeof+msdn.microsoft.com(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  no i want import option directly from xml to access

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
  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 http://www.aspfree.com/c/a/Microsoft-Access/Importing-XML-into-MS-Access-2003/1/ for more info on this
  Reena Jain replied to Santhosh N
06-Feb-10 04:48 AM
no i have to do it with code only
  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...
  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
help
never gets into the foreach ( DataRow row in xmlt.Rows) DataSet xmlds = new DataSet (); xmlds.ReadXml( "c: / projects / air ambulance / aircraft.xml" , XmlReadMode .ReadSchema); DataTable xmlt = new DataTable (); xmlt = xmlds.Tables[ "Resources" ]; string XML_STMT = "" ; foreach ( DataRow row in xmlt.Rows) { string XML_CONGRPID = row ToString(); string XML_UNIT = row[ "RESOURCEID" ].ToString(); XML_STMT = " AND UNIT = " + XML_UNIT; } Hi, You can use the ReadXml method to read XML schema and data into a DataSet. XML data can be read a TextWriter object. You can use one of two sets of overloaded methods for the ReadXml method, depending on your needs. The first set of four overloaded methods takes just one a specified file to read XML schema and data into the DataSet:Overloads Public Sub ReadXml(String) • The code to follow uses a specified TextReader to read XML schema and data into the DataSet. TextReader is designed for character input.Overloads Public Sub ReadXml(TextReader) • The code to follow uses a specified System.IO.Stream to read XML schema the DataSet. The Stream class is designed for byte input and output.Overloads Public Sub ReadXml(Stream) • The code to follow uses a specified XmlReader to read XML schema and data
want to create a dataset from a xml file (stored in a folder) thnx Using ReadXml method of Dataset you can load XMl file in Dataset. Use this code DataSet ds = new DataSet(); ds.ReadXml("xml path"); After excution of this code xml file will be loaded to dataset. thnx a lot. Hi, just use this method for read xml file in datatable in C#.net, which is working for me public DataTable ReadXML( string file) { / / create the DataTable that will hold the data DataTable table = new DataTable( "XmlData" ); try { / / open the file using a Stream using (Stream stream = new FileStream(file, FileMode
it, and parsing the contents of the XML file and adding the contents into a datatable? The scenario for this project is that emails containing XML file attachments (one attachment per table. If I can get the contents of the XML file attachment read into a datatable in my c# app, I can take things from there. Prolly the easiest option for codeproject.com / KB / IP / POP3ClientByChadZHower.aspx To convert the XML in the attachment to a DataTable, adapt this snippet - public DataTable ReturnDataTbl(string XmlFile) { DataSet dset = new DataSet(); FileStream fstr = new FileStream(XmlFile, FileMode.Open, FileAccess.Read); dset.readXml(fstr); DataTable dtbl = dset.Tables[0]; fstr.Close(); dset.Dispose(); return dtbl
read xml using dataset in asp.net here is the code for this public static DataTable GetProducts () { DataSet dsStore = new DataSet () ; dsStore.ReadXml ( "myXmlFile.xml" ) ; return dsStore.Tables [ "Employee" ] ; } Hope this will help you You need to create reader = new XmlTextReader ( "Example.xsd" ); XmlSchema myschema = XmlSchema .Read(reader, null ); myschema.Write( Console .Out); FileStream file = new FileStream ( "New.xsd" , FileMode .Create, FileAccess .ReadWrite); XmlTextWriter xwriter = new XmlTextWriter (file, new UTF8Encoding ()); xwriter.Formatting = Formatting .Indented; / / myschema.Write(xwriter sender, System.EventArgs e) { string myXMLfile = @ "C: \ MySchema.xml" ; DataSet ds = new DataSet(); / / Create new FileStream with which to read the schema. System.IO.FileStream fsReadXml = new System.IO.FileStream (myXMLfile, System.IO.FileMode.Open); try { ds.ReadXml(fsReadXml); dataGrid1
Export XML document to DATATABLE May I have your helping hands in order to export XML Document to DATATABLE with and without using loop. thanks a lot. -eros Makati, PH Try if it is useful, This is in C# FileStream findata = new FileStream("userdata.xml", FileMode.Open FileAccess.Read, FileShare.ReadWrite); / / Read the DataBase into the DataSet ds.ReadXmlData(findata); From ds You can have datatable. . . . I tried that but the URI format is not supported. by URI format? Dim findata As System.IO.FileStream = New System.IO.FileStream("http: / / localhost / PrintCtr / xmldoc .TblName.XML", IO.FileMode.Open, IO.FileAccess