我用下面代码导出了一个xml ,可是中文是非GBK正常内容,查看源码的时候出现一些乱码 ,不知道为什么dim test As New ADODB.Recordset
test.Open "Selct * From Table1"
test.Save "C:\aa.xml", adPersistXML
test.Open "Selct * From Table1"
test.Save "C:\aa.xml", adPersistXML
Dim Rs As New ADODB.Recordset
CN.ConnectionString = "Provider=sqloledb;Data Source=pmserver;Initial Catalog=northwind;User Id=sa;Password=sa;" CN.Open
Rs.CursorLocation = adUseClient
Rs.Open "select * from employees", CN, adOpenDynamic, adLockOptimistic Rs.Save "C:\aa.xml", adPersistXML用引用的是2.5,正常!!
所以xml里的中文是经过编码后的形式
有些xml文件中中文会按 Unicode 编码的方式(但这也不是乱码)
我从网络上下载了 几个 wap 页面 看了一下,他的文件里的中文就是显示的Unicode编码.而不是中文...
所以我写了两个函数 来转换
Private Function Covert(strIn As String) As String
'Unicode 转换为 中文函数
Dim i As Long
Dim bt(1) As Byte
Dim hr As String
For i = 1 To Len(strIn) Step 4
bt(1) = Val("&H" & Mid(strIn, i, 2))
bt(0) = Val("&H" & Mid(strIn, i + 2, 2))
hr = bt
Covert = Covert & hr
Next
End FunctionPrivate Function StrToUnicode(strIn As String) As String
'中文
Dim buf() As Byte
Dim i As Long
buf = StrConv(strIn, vbUnicode)
For i = 0 To UBound(buf()) Step 2
StrToUnicode = Hex(buf(i)) & StrToUnicode
Next
End Function
'比如欢迎 的 '欢' 在xml文件里 就可能是 欢
'StrToUnicode("欢") = "6B22"