我想在VB中将几张表生成一个XML文件,却找不到方法,请高手指点一下。谢谢。
我想在VB中将几张表生成一个XML文件,却找不到方法,请高手指点一下。谢谢。
运行Print #1, "<field name=""" & s_FieldName(j) & """> </field>"
是没有问题的。
但我要运行
Print #1, "<field name=""" & s_FieldName(j) & """> " & rs.Fields(j).Value & "</field>"
就会出现问题:
文本内容中发现无效字符
<field name="sname">
msgbox "<field name=""" & s_FieldName(j) & """> " & rs.Fields(j).Value & "</field>"
运行是没有错,但我要写在XML文件中。
把msgbox换为print #1, 就会出现错误了。
提示是:
文本内容中发现无效字符。
<field name="sname">
测试通过。代码为:
Option Explicit
Private Sub Form_Load()
Dim conn As New ADODB.Connection
Dim ConString As String
Dim strTableName As String
Dim str As String
Dim sa As String
Dim pwd As String
Dim data1e As String
str = "192.168.0.1"
pwd = ""
data1e = "wh3"
ConString = "driver={sql server}; server=" & str & ";uid=sa ; pwd=" & pwd & ";database=" & data1e & ""
conn.Open ConString
Open "f:\d\x.xml " For Output As #1
Print #1, "<?xml version=""1.0"" encoding=""gb2312"" ?>"
Print #1, "<result>"
Close
strTableName = "student"
toXml strTableName, conn, "主表"
strTableName = "uses"
toXml strTableName, conn, "副表"
conn.Close
Open "f:\d\x.xml " For Append As #1
Print #1, "</result>"
Close
End Sub
Private Sub toXml(tableName As String, con As ADODB.Connection, t As String)
Dim rs As New ADODB.Recordset
Dim i As Integer
Dim s As Integer
Dim j As Integer
Dim str As String
Dim ss As Variant
str = "select * from " & tableName & ""
rs.CursorLocation = adUseClient
rs.Open str, con, adOpenKeyset, adLockReadOnly, adCmdText
s = rs.Fields.Count
ReDim s_FieldName(s) As String
For i = 0 To s - 1
s_FieldName(i) = rs.Fields(i).Name
Next
Open "f:\d\x.xml" For Append As #1
Print #1, "<table Name = """ & tableName & "为 " & t & """> "
For i = 0 To rs.RecordCount - 1
Print #1, "<row>"
For j = 0 To s - 1
ss = rs.Fields(j).Value
Print #1, "<field name=""" & s_FieldName(j) & """ value=""" & ss & """></field>"
Next
Print #1, "</row>"
rs.MoveNext
Next
Print #1, "</table>"
Close
rs.Close
End Sub
[此贴子已经被作者于2007-9-19 15:47:01编辑过]