ARTICLE

Transfer data from MS Access to XML

Posted by Sunitha Padmasri Pamart Articles | XML in VB.NET August 27, 2008
This example illustrates how to access data from MS Access and create an XML file and write the data into it.
Download Files:
 
Reader Level:

This below source code shows how to transfer data from MS Access database to an XML file. Change the XML file name and your database connection string in below code and run it.

 

Imports System.Data.OleDb

Imports System.Xml

Public Class Form1

Dim strAcessFilePath As String

Dim strXmlFilePath As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

strAcessFilePath = System.Windows.Forms.Application.StartupPath & "\DataReport.mdb"

strXmlFilePath = "C:\CustomerData.xml"

GetDataFromAcessFile()

End Sub

Private Sub GetDataFromAcessFile()

Dim aCon As OleDbConnection

Dim aCmd As OleDbCommand

Dim strCon As String

Dim strQuery As String

'create connection with mdb file

strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strAcessFilePath & ";User ID=Admin;Password="

aCon = New OleDbConnection(strCon)

aCon.Open()

strQuery = "Select * from Customers"

aCmd = New OleDbCommand(strQuery, aCon)

Dim da As New OleDbDataAdapter

da.SelectCommand = aCmd

Dim ds As DataSet

ds = New DataSet("Customers")

da.Fill(ds, "New Customers")

Dim dt As DataTable

dt = New DataTable()

dt = ds.Tables("New Customers")

CreateXMLFile()

WriteToXml(dt)

aCon.Close()

End Sub

Private Sub WriteToXml(ByVal Acessdt As DataTable)

'Write the elements to the file

If Dir(strXmlFilePath).Length = 0 Then

CreateXMLFile()

End If

Dim XmlDoc As New XmlDocument

Dim xNode As XmlNode

Dim xElement As XmlElement

Dim NewxElement As XmlElement

XmlDoc.Load(strXmlFilePath)

If XmlDoc.DocumentElement.HasChildNodes = False Then

Dim inti As Integer

Dim intCol As Integer

'Add the node here

For inti = 1 To Acessdt.Rows.Count - 1

xNode = XmlDoc.CreateNode(XmlNodeType.Element, "customer", Nothing)

xElement = XmlDoc.DocumentElement.AppendChild(xNode)

xNode = Nothing

For intCol = 0 To Acessdt.Columns.Count - 1

xNode = XmlDoc.CreateNode(XmlNodeType.Element, Acessdt.Columns(intCol).Caption, Nothing)

NewxElement = xElement.AppendChild(xNode)

NewxElement.SetAttribute("value", Acessdt.Rows.Item(inti).Item(intCol))

Next intCol

Next

End If

XmlDoc.Save(strXmlFilePath)

End Sub

Private Sub CreateXMLFile()

'Create an XML File

If Dir(strXmlFilePath).Length <> 0 Then

Kill(strXmlFilePath)

End If

Dim xTextWriter As New XmlTextWriter(strXmlFilePath, Nothing)

xTextWriter.WriteStartDocument() 'this line writes the version <?xml version="1.0" ?>

xTextWriter.WriteComment("This file consists of Customers data")

xTextWriter.WriteComment("This data is collected from Access Customers table.")

xTextWriter.WriteStartElement("Customers")

xTextWriter.WriteEndElement()

xTextWriter.Flush()

xTextWriter.Close()

End Sub

End Class

Login to add your contents and source code to this article
share this article :
post comment
 

Hello everybody i would like to ask you about replace two string into one textbox to number. eg. i have txt1=$ 1,200. i want in txt1=1200. how can i do if want it in function?

Posted by Meas Dymong Feb 06, 2011

i am a beginner in xml. please, give me information each of code above.
i had problem. and i understand yet about all that. ex: i have database "BANK" in sql server. and i have form in vb .net . how can i get and display this data in datagrid after pressed the button? please sir.. help me about this.

Posted by cone first Sep 30, 2010

Yes we can add the fields as elements too.
You first create a node and delcare it as type element. Then add this node to the parent node.  I have taken only 2 fields from the database Last name and first name.


Earlier i was adding the fields as attributes.

- <customer>
  <LastName value="Holford" />
     <FirstName value="Raymond" />

But now when the fields are added as elements i got like this 

 - <customer>
       <LastName>
              <Holford />
     </LastName>
       <FirstName>
              <Raymond />
     </FirstName>
 </customer>

The following is the code for it.



For intCol = 0 To Acessdt.Columns.Count - 1

   Dim xSubNode As XmlNode

   Dim strTemp As String

   xNode = XmlDoc.CreateNode(XmlNodeType.Element, Acessdt.Columns(intCol).Caption, Nothing)

   strTemp = Acessdt.Rows.Item(inti).Item(intCol)

   xSubNode = XmlDoc.CreateNode(XmlNodeType.Element, strTemp, Nothing)

   xNode.AppendChild(xSubNode)

   NewxElement = xElement.AppendChild(xNode)

Next intCol


Hope i answered your question.

Posted by Sunitha Padmasri Pamart Jul 14, 2009

Yes you can transfer any selected fields to xml.

All you need to do is pass the query with the selected fields. The query string in located in GetDataFromAcessFile procedure.

Eg:    strQuery = "Select LastName, FirstName from Customers"

Here i have selected the lastname and the first name fields from the acess database file. Now only these fields will be written into the xml file.

- <customer>
  <LastName value="Holford" />
  <FirstName value="Raymond" />
  </customer>
- <customer>
  <LastName value="Collin" />
  <FirstName value="Joy" />
  </customer>

Posted by Sunitha Padmasri Pamart Jul 14, 2009

in the line " NewxElement.SetAttribute("value", Acessdt.Rows.Item(inti).Item(intCol))" you have wrote the filds as attributes in xml doc but can we insert them as elements? wat is the code to do it??

Posted by mathu mathi Mar 31, 2009
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor