CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2001
    Posts
    7

    Export to XML file

    Hi,

    Any idea how to export data in MS Access to XML file using VB?

    Thanks.


  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Export to XML file

    You can do this in a few ways.

    1) You can use ADO, as of version 2.5, you can save an ADO recordset to XML. This however, gives you XML in the following way
    ...
    <row name=Cakkie location=Belgium age=unknown />
    <row name=Cimperiali location=Italy age=old />
    ...


    this can be done with the save method, either directing it to a file, or directing it to a stream object.

    2) you can use the XML DOM object. i'm not sure how this works, but you can get an XML structure like this
    <PERSON>
    <NAME>Cakkie</NAME>
    <LOCATION>Belgium</LOCATION>
    <AGE>unknown</AGE>
    </PERSON>
    <PERSON>
    <NAME>Cimperiali</NAME>
    <LOCATION>italy</LOCATION>
    <AGE>old</AGE>
    </PERSON>



    3) You can get the same structure as above, doing it entirely in VB, that is, opening the recordset, loop through it, and write your data to a textfile in XML.
    Your code should look something like this

    private Sub WriteXML(rst as Recordset, Filename as string)
    dim FFile as integer
    FFile = freefile
    open filename for output as #ffile

    ' xml header
    print #ffile, "<xml>" & vbcrlf & " <records>" & vbcrlf

    ' loop through each record
    do until rst.eof
    print #ffile, " <record>"
    dim fld as field
    ' loop through each field an output it to the file
    for each fld in rst.fields
    print #ffile, space(6) & "<" & fld.name & ">" & fld.value & "</" & fld.name & ">" & vbcrlf
    next fld

    ' close the record
    print #ffile, " </record>" & vbcrlf

    rst.movenext

    loop

    ' close the xml file
    print #ffile, " </records>" & vbcrlf & "</xml>"
    close #ffile

    End Sub






    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Export to XML file

    You can create a recordset and save it in XML format


    Rst1.Save "c:\Temp\Recordset.xml", adPersistXML

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured