如何使用vb.net从数据库创建XML文件 正如我尝试的那样,我只创建了根元素而不是数据库中的任何数据,而我的代码是:
<% @Import Namespace="System" %>
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.xml" %>
<% @Import Namespace="System.Data.SqlClient" %>
<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()
End If
End Sub
</script>
我得到的输出只是一个XML文件,只用上面描述的根元素创建,而不是数据库中的数据。
如何从数据库中获取数据并形成XML文件?
答案 0 :(得分:0)
尝试使用WriteXml方法,如下所示:
Private Sub WriteXmlToFile(thisDataSet As DataSet)
If thisDataSet Is Nothing Then
Return
End If
' Create a file name to write to.
Dim filename As String = "XmlDoc.xml"
' Create the FileStream to write with.
Dim stream As New System.IO.FileStream _
(filename, System.IO.FileMode.Create)
' Write to the file with the WriteXml method.
thisDataSet.WriteXml(stream)
End Sub
您可以在MSDN上找到有关DataTable.WriteXml和DataSet.WriteXml的更多信息。
答案 1 :(得分:0)
所以你应该问过从数据库中检索数据&#39; ??
如何更改sql语句:
sql =&#34;从jb_jobs中选择*其中city =&#39; Los Angeles&#39; &#34;
到
sql =&#34;从jb_jobs中选择*城市LIKE&#39; Los Angeles&#39;&#34; - &GT;串
或
sql =&#34;从jb_jobs中选择*,其中city = CityID&#34; - &GT;整数