C# .NET - DBF file to XML - Asked By sreejith s on 11-Feb-08 06:26 PM

hi to all,

hi i need some codes from .net to convert DBF file to XML file automatically every 3 hours.hope some one will help me.

Thanks in advance.

Cheers

Sreejith

MCAD

The easiest way to do that... - Nenad Prekupec replied to sreejith s on 11-Feb-08 06:55 PM

If you load your dbf file into DataSet (using ODBC driver), you can then call WriteXml method of DataSet class and have your xml file of DataSet.
As Xml is hierarchy set i'm sure it won't be problem.. Eventually you can port that xml back to DataSet on other machine and store to MS Sql without problem.

DBF to xml - sundar k replied to sreejith s on 11-Feb-08 08:45 PM

Try this code, you can change the code segments as per your requirement

using System.Data.OleDb;
private OleDbConnection conn;
private OleDbCommand cmd;
private OleDbDataReader dr;

      conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=[SOMEPATH TO        THE DBF FILE];Extended Properties=DBASE III;");

      conn.Open();
      sqlStr = "Select * from [YourDBFFile.DBF]";

 //Make a DataSet object
      myDataSet = new DataSet() ;
      //Using the OleDbDataAdapter execute the query
      myAdapter = new OleDbDataAdapter( sqlStr , conn ) ;
      //Build the Update and Delete SQL Statements
      OleDbCommandBuilder myBuilder = new OleDbCommandBuilder( myAdapter );
      try
      {
        //Fill the DataSet with the Table 'bookstock'
        myAdapter.Fill(myDataSet,"somename") ;
   // Get a FileStream object
       FileStream myFs =
        new FileStream
        ("myXmlData.xml",FileMode.OpenOrCreate,FileAccess.Write);

 // Use the WriteXml method of DataSet object to write an XML file from the DataSet
       myDs.WriteXml(myFs);
       myFs.Close();
      }

conn.Close();

YOu can create the above code as Windows Application and build a exe out of it, create a task in windows scheduler which can run for every 3 hours.

DBF to XML - sreejith s replied to sundar k on 13-Feb-08 12:18 PM

hi sunder,

Thanks for ur response for my request and appreciated ur time for my code.

hey , i tried to run ur code,getting so many errors.will u pls explain how to run this?

thanks

sreejith

try this code! - sundar k replied to sreejith s on 16-Feb-08 12:34 PM


I hvae corrected the code and hope your errors will get reduced! I dont have VS.NET at home to help you out in correcting compilation errors, I have just typed it in the notepad, just try it out from yoru end and hope there wont be much errors.Sorry couldnt help you much!

using System.Data.OleDb;
using System.IO;
class MyClass {

 private OleDbConnection conn;
 private OleDbCommand cmd;
 private OleDbDataReader dr;
 private string sqlStr = "";
 private DataSet myDataSet;
 private leDbDataAdapter myAdapter;

 public static void Main()
 {
  Execute();
 }

 public void Execute()
 {

       conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=[SOMEPATH TO         THE DBF FILE];Extended Properties=DBASE III;");
 conn.Open();
      sqlStr = "Select * from [YourDBFFile.DBF]";

  //Make a DataSet object
       myDataSet = new DataSet() ;
       //Using the OleDbDataAdapter execute the query
       myAdapter = new OleDbDataAdapter( sqlStr , conn ) ;
       //Build the Update and Delete SQL Statements
       OleDbCommandBuilder myBuilder = new OleDbCommandBuilder( myAdapter );
        try
        {
         //Fill the DataSet with the Table 'bookstock'
         myAdapter.Fill(myDataSet,"somename") ;
     // Get  a FileStream object
         FileStream myFs =
          new FileStream 
          ("myXmlData.xml",FileMode.OpenOrCreate,FileAccess.Write);

  // Use the WriteXml method of DataSet object to write XML file from the DataSet
         myDs.WriteXml(myFs);
         myFs.Close();
  conn.Close();
        }
  catch(Exception oEx)
  {
   throw(oEx);
  }
 }
}


YOu can create the above code as Windows Application and build a exe out of it, create a task in windows scheduler which can run for every 3 hours.

thanx for this code Sundar, but i have a few questions for you - A D replied to sundar k on 13-May-09 08:14 AM

In my company we get several new dbf files into different folders to the server every day.
These dbf files has to convert automatic to XML files as soon as possible they arrive.

I think this code can help me, but how can it locate each file? Must all files be in same folder?
Is it difficult to create this code as a Windows Application, or is it easyer to have it on the server with a cronjob f.ex?

I also have to exclude some colums in the dbf files.


best regards

Are