2013-08-01 95 views
1

如何使用vb.net從數據庫創建XML文件 正如我試過,我只得到了創建的根元素,而不是數據庫中的任何數據,我的代碼是:如何在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

嘗試使用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.WriteXmlDataSet.WriteXml

+0

我在創建XML文件時沒有任何問題,但問題是從數據庫中檢索值並將其保存到XML文件 – ammukarthi

0

所以你應該問'從數據庫中檢索數據'?

你怎麼樣改變你的SQL語句:

SQL = 「從jb_jobs選擇*其中城市=」洛杉磯「

SQL =「從jb_jobs選擇*其中城市像'洛杉磯」' - >字符串

SQL = 「從jb_jobs選擇*,其中城市= CityID」 - >整數