Click here to Skip to main content
15,923,168 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create XML file from database table using vb.net
As i tried my code is:

XML
<% @Import Namespace="System" %>
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.xml" %>
<% @Import Namespace="System.Data.SqlClient" %>
<% @Import Namespace="System.xml.xmlNode" %>
<Script runat="server">
sub page_load
        Dim connectionString As String
        Dim connection As SqlConnection
        Dim adapter As SqlDataAdapter
        Dim ds  As New DataSet
        Dim sql As String

        connectionString = ///my connection string////
        connection = New SqlConnection(connectionString)

        sql = "select * from jb_jobs where city='Los Angeles' "
        connection.Open()
            adapter = New SqlDataAdapter(sql, connection)
            adapter.Fill(ds)
            connection.Close()
        If IO.File.Exists("product.xml") = False Then
            Dim settings As New XmlWriterSettings()
            settings.Indent = True
        Dim XmlWrt As XmlWriter = XmlWriter.Create("c:/xmlfiles/product.xml", settings)
        XmlWrt  .WriteStartDocument()
        XmlWrt  .WriteComment("XML Database.")
        XmlWrt  .WriteStartElement("source")
        XmlWrt  .WriteStartElement("jobs")



        XmlWrt  .WriteEndElement()
        XmlWrt  .WriteEndDocument()
        XmlWrt  .Close()
        Response.write("XML File Created")
        End If
        End Sub

</script>


and my output is just a XML file is created with the root elements but not with the data's from database
How to resolve this????
Posted

I got the output after a long struggle
and the code is:

XML
<% @Import NameSpace="System.Data" %>
<% @Import NameSpace="System.Data.SqlClient" %>
<Script runat="Server">
Sub Page_Load
    Dim connetionString As String
    Dim connection As SqlConnection
    Dim adapter As SqlDataAdapter
    Dim ds As New DataSet
    Dim sql As String

    connetionString = ///my connection string///
    connection = New SqlConnection(connetionString)
    sql = "select * from det where city='Los Angeles' "
    Try
        connection.Open()
        adapter = New SqlDataAdapter(sql, connection)
        adapter.Fill(ds)
        connection.Close()
        ds.WriteXml("C:\xmlfiles\xmldata.xml")
        Console.WriteLine("Done")
    Catch ex As Exception
        Console.WriteLine(ex.ToString)
    End Try
    End Sub
</script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900