从VBA中的文件加载DataSet时出现错误。
?RsLoadFromXML( “C:\ sletmig \的test.xml”)
无法创建Recordset。源XML不完整或无效。
这是我的源代码
Function RsLoadFromXML(sPath As String) As Recordset
Dim oStream As ADODB.Stream, oRsLoad As ADODB.Recordset
On Error GoTo ErrFailed
Set oRsLoad = New ADODB.Recordset
oRsLoad.CursorLocation = adUseClient
Set oStream = New ADODB.Stream
oStream.Charset = "iso-8859-1"
oStream.Open
'Load stream
oStream.LoadFromFile sPath
'Open recordset using stream
oRsLoad.Open oStream
Set RsLoadFromXML = oRsLoad
oStream.Close
Set oStream = Nothing
Set oRsLoad = Nothing
Exit Function
ErrFailed:
Debug.Print Err.Description
Debug.Assert False
Set RsLoadFromXML = Nothing
End Function
尝试加载此test.xml文件
<?xml version="1.0"?>
<Instruments>
<Control>
<AnalysisNotional>1000000</AnalysisNotional>
<TAsOf>18/08/2011</TAsOf>
<NumberOfDays>1</NumberOfDays>
</Control>
</Instruments>
答案 0 :(得分:0)
@Tim Williams是正确的... ADO可以处理的XML专门用于Recordset.Save(see this)创建的持久记录集。
如果您错过了精细打印,那么来自Microsoft的This article可能会产生误导:“如果XML数据采用ADO记录集可以接受的格式。”
如果要加载通用XML文件,请使用Microsoft XML(在Office中,您需要添加对“Microsoft XML#。#”的引用。您可以在工具 - &gt; Visual Basic编辑器中的引用中执行此操作。)
Sub XML()
Dim oDoc As MSXML2.DOMDocument
Set oDoc = New MSXML2.DOMDocument
oDoc.Load ("C:\Path\To\File\AnXMLFile.xml")
End Sub
如果您需要更多信息,请告诉我们。
编辑:您每天都在学习新知识。这实际上是非常酷的恕我直言。由于您需要记录集中的数据,因此可以创建没有后备数据库的记录集并向其中添加数据。 (Source article)
Sub XML()
Dim oDoc As MSXML2.DOMDocument
Dim oRS As ADODB.Recordset
Dim oNode As MSXML2.IXMLDOMNode
Dim oSubNodes As MSXML2.IXMLDOMSelection
Set oDoc = New MSXML2.DOMDocument
oDoc.Load ("C:\Path\To\File\AnXMLFile.xml")
Set oRS = New ADODB.Recordset
Set oRS.ActiveConnection = Nothing
oRS.CursorLocation = adUseClient
oRS.LockType = adLockBatchOptimistic
With oRS.fields
.Append "AnalysisNotional", adInteger
.Append "TAsOf", adDate
.Append "NumberOfDays", adInteger
End With
oRS.Open
For Each oNode In oDoc.selectNodes("/Instruments/Control")
oRS.AddNew
oRS.fields("AnalysisNotional").Value = oNode.selectSingleNode("AnalysisNotional").Text
oRS.fields("TAsOf").Value = oNode.selectSingleNode("TAsOf").Text
oRS.fields("NumberOfDays").Value = oNode.selectSingleNode("NumberOfDays").Text
Next
oRS.MoveFirst
'--- Do something with recordset here ---
End Sub
答案 1 :(得分:0)
谢谢。我试图创建一些示例代码,XML似乎很奇怪,所以我会尝试你的建议。
Sub Create_recordset()
Dim r As ADODB.Recordset
Dim strFileName As String
strFileName = "d:\sletmig\test.txt"
Set r = New ADODB.Recordset
'Add Columns
r.Fields.Append "Field1", adVarWChar, 50
r.Fields.Append "Field2", adVarWChar, 50
'Create Recordset
r.Open
'Add rows into recordset
r.AddNew Array("field1", "field2"), Array("string1", “val1”)
r.AddNew Array("field1", "field2"), Array("string2", “val2”)
On Error Resume Next
Kill strFileName
r.Save strFileName, adPersistXML
r.Close
Set r = Nothing
End Sub
给我这个xml文件
<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
<s:ElementType name='row' content='eltOnly' rs:updatable='true'>
<s:AttributeType name='Field1' rs:number='1' rs:write='true'>
<s:datatype dt:type='string' dt:maxLength='50' rs:precision='0' rs:maybenull='false'/>
</s:AttributeType>
<s:AttributeType name='Field2' rs:number='2' rs:write='true'>
<s:datatype dt:type='string' dt:maxLength='50' rs:precision='0' rs:maybenull='false'/>
</s:AttributeType>
<s:extends type='rs:rowbase'/>
</s:ElementType>
</s:Schema>
<rs:data>
<rs:insert>
<z:row Field1='string1' Field2=''/>
<z:row Field1='string2' Field2=''/>
</rs:insert>
</rs:data>
</xml>