how to create xml file from a database table using vb.net
I tried a code but it doesn't retrive values from the database table
How to resolve this
And my code is:

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1"  Debug="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<% @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 = "Data Source=IMMANUEL-PC\SQLEXPRESS; Initial Catalog=mbnbv; UID=xxx; PWD=xxx;" 
        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)
        XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        XmlWrt  .WriteStartDocument()
        XmlWrt  .WriteComment("XML Database.")
        XmlWrt  .WriteStartElement("source")
        XmlWrt  .WriteStartElement("jobs")

                XmlNode partnerJobIdNode = doc.CreateElement("partner-job-id");
                partnerJobIdNode.AppendChild(doc.CreateTextNode(reader["jobid"].ToString()));
                jobNode.AppendChild(partnerJobIdNode);


                 XmlNode contractIdNode = doc.CreateElement("contract-id");
                contractIdNode.AppendChild(doc.CreateTextNode(reader["EmployeeTypeId"].ToString()));
                jobNode.AppendChild(contractIdNode);

                XmlNode customerJobCodeNode = doc.CreateElement("customer-job-code");
                customerJobCodeNode.AppendChild(doc.CreateTextNode(reader["UserID"].ToString()));
                jobNode.AppendChild(customerJobCodeNode);

                XmlNode companyNameNode = doc.CreateElement("company-name");
                companyNameNode.AppendChild(doc.CreateTextNode(reader["Company"].ToString()));
                jobNode.AppendChild(companyNameNode);

                XmlNode titleNode = doc.CreateElement("title");
                titleNode.AppendChild(doc.CreateTextNode(reader["jobTitle"].ToString()));
                jobNode.AppendChild(titleNode);

                XmlNode descriptionNode = doc.CreateElement("description");
                descriptionNode.AppendChild(doc.CreateTextNode(reader["Description"].ToString()));
                jobNode.AppendChild(descriptionNode);

                XmlNode skillNode = doc.CreateElement("skill");
                skillNode.AppendChild(doc.CreateTextNode(reader["skill"].ToString()));
                jobNode.AppendChild(skillNode);

                XmlNode countryNode = doc.CreateElement("country");
                countryNode.AppendChild(doc.CreateTextNode(reader["country"].ToString()));
                jobNode.AppendChild(countryNode);


                XmlNode payNode = doc.CreateElement("pay");
                payNode.AppendChild(doc.CreateTextNode(reader["Pay"].ToString()));
                jobNode.AppendChild(payNode);

                XmlNode contactemailNode = doc.CreateElement("contactemail");
                contactemailNode.AppendChild(doc.CreateTextNode(reader["Contactemail"].ToString()));
                jobNode.AppendChild(contactemailNode);


                XmlNode cityNode = doc.CreateElement("city");
                cityNode.AppendChild(doc.CreateTextNode(reader["city"].ToString()));
                jobNode.AppendChild(cityNode);

        XmlWrt  .WriteEndElement()
        XmlWrt  .WriteEndDocument()
        XmlWrt  .Close()
        End If
        End Sub
</script>

</body>
</html>

and the error thrown on the code is:
BC30108 : XmlNode is a type and cannot be used as an expression

Recommended Answers

All 2 Replies

Not being able able to fully understand your question, it was, quite, complicated. If just some tables, why don't you add your tables into a dataset then use:

ds.WriteXml("C:\BackUp\New.xml")

I got the output for the above problem and the code is:

<% @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 edt 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>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.