我想要做的是將XML字符串轉儲到RecordSet中。我遇到的問題是,如果我首先將XML字符串保存到文件中,然後從我認爲是多餘的文件中讀取,代碼似乎工作正常。但是,當我想從字符串讀取時,出現錯誤幫助將XML字符串傳遞給RecordSet
RecordSet無法創建。源XML不完整或無效。 80004005
我的XML字符串的形式是
<portfolio>
<stock>
<shares>100</shares>
<symbol>MSFT</symbol>
<price>$70.00</price>
<info>
<companyname>Microsoft Corporation</companyname>
<website>http://www.microsoft.com</website>
</info>
</stock>
<stock>
<shares>100</shares>
<symbol>AAPL</symbol>
<price>$107.00</price>
<info>
<companyname>Apple Computer, Inc.</companyname>
<website>http://www.apple.com</website>
</info>
</stock>
<stock>
<shares>100</shares>
<symbol>DELL</symbol>
<price>$50.00</price>
<info>
<companyname>Dell Corporation</companyname>
<website>http://www.dell.com</website>
</info>
</stock>
<stock>
<shares>100</shares>
<symbol>INTC</symbol>
<price>$115.00</price>
<info>
<companyname>Intel Corporation</companyname>
<website>http://www.intel.com</website>
</info>
</stock>
</portfolio>
,我使用的XML字符串轉換的代碼(即我有問題)是
Public Function RecordsetFromXMLString(sXML As String) As Recordset
Dim oStream As ADODB.Stream
Set oStream = New ADODB.Stream
oStream.Open
oStream.WriteText sXML 'Give the XML string to the ADO Stream
oStream.Position = 0 'Set the stream position to the start
Dim oRecordset As ADODB.Recordset
Set oRecordset = New ADODB.Recordset
oRecordset.Open oStream 'Open a recordset from the stream
oStream.Close
Set oStream = Nothing
Set RecordsetFromXMLString = oRecordset 'Return the recordset
Set oRecordset = Nothing
End Function
請,對你的幫助表示感謝。
http://msdn.microsoft.com/en-us/library/ms810621
http://support.microsoft.com/kb/263247
我已經使用這個下面
Public Function RecordsetFromXMLDocument(XMLDOMDocument)
Dim oRecordset
Set oRecordset = CreateObject("ADODB.Recordset.6.0")
oRecordset.Open XMLDOMDocument 'pass the DOM Document instance as the Source argument
Set RecordsetFromXMLDocument = oRecordset 'return the recordset
Set oRecordset = Nothing
End Function
嘗試,但仍然遇到了同樣的問題。
代碼我用我的DomDocumentData格式轉換成ADO的XML持久格式我需要這種
'*******************************************************************************************
' SCHEMA GENERATOR
'*******************************************************************************************
'parentnodepath -- XPath to the Main Node/Table/RowCollection
'parentnodepath -- Name of the Main Node/Table/RowCollection
Function CreateSchemafromNode(XMLDocument,parentnodepath, parentnodeName)
Dim schema, stemp, MyArray,nodename, childnodelist,counter, n, x, tempnode
schema = TextWriterSchemaNameSpaceHeader()
schema=schema & TextWriterSchemaHeader(parentnodeName)
'LOOP HERE
counter = 0
For Each stemp In XMLDocument.SelectSingleNode(parentnodePath).ChildNodes(0).ChildNodes
counter = counter + 1
schema = schema & TextWriterSchemaRowAttributeElement(stemp.NodeName, counter, "")
Next
'END LOOOP HERE
schema=schema & TextWriterSchemaSchemaEnd
schema =schema & TextWriterSchemaRowHeader
'BEGIN FIRST LOOP HERE -- FOR EACH TOP NODE --ROW
For Each n In XMLDocument.SelectSingleNode(parentnodePath).ChildNodes
schema =schema & TextWriterSchemaAddRowBegin()
'BEGIN SECOND LOOP HERE -- FOR EACH CHILD OF TOP NODE -- NODE VALUE IN CURRENT ROW -- FOR EACH CHILDNODELIST NAME
For Each x In n.ChildNodes
schema = schema & TextWriterSchemaAddRowFieldNameValue(x.NodeName, x.text)
'END SECOND LOOP HERE
Next
schema =schema & TextWriterSchemaAddRowEnd()
Next
'END FIRST LOOP HERE
schema =schema & TextWriterSchemaRowEnd()
schema =schema &TextWriterSchemaNameSpaceEnd()
CreateSchemafromNode=schema
End Function
Function TextWriterSchemaNameSpaceHeader()
Dim schemaString
schemaString= "<xml xmlns:s='"
schemaString= schemaString & "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882' " & vbCrLf & vbTab
schemaString= schemaString & ("xmlns:dt='")
schemaString= schemaString & "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882' " & vbCrLf & vbTab
' schemaString= schemaString & ("xmlns:dt='")
' schemaString= schemaString & "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882' " & vbCrLf & vbTab
schemaString= schemaString & "xmlns:rs='urn:schemas-microsoft-com:rowset' " & vbCrLf & vbTab
schemaString=schemaString & " xmlns:z='#RowsetSchema'> " & vbCrLf
TextWriterSchemaNameSpaceHeader = schemaString
End Function
Function TextWriterSchemaHeader(recordname)
Dim schemaString
schemaString= "<s:Schema id='RowsetSchema'>"& vbCrLf & vbTab
schemaString= schemaString &"<s:ElementType name='" & recordname & "' content='eltOnly'>" & vbCrLf
TextWriterSchemaHeader = schemaString
End Function
Function TextWriterSchemaRowAttributeElement(rowname, rowordernumber, rowtype)
Dim schemaString
schemaString=vbTab & vbTab & "<s:AttributeType name='" & rowname & "' rs:number='" & rowordernumber & "' />" & vbCrLf
TextWriterSchemaRowAttributeElement = schemaString
End Function
Function TextWriterSchemaSchemaEnd()
Dim schemaString
schemaString=vbTab & vbTab & "<s:extends type='rs:rowbase'/>" & vbCrLf
schemaString= schemaString &vbTab & "</s:ElementType>" & vbCrLf
schemaString= schemaString & "</s:Schema>" & vbCrLf
TextWriterSchemaSchemaEnd =schemaString
End Function
Function TextWriterSchemaRowHeader()
Dim schemaString
schemaString= vbTab & "<rs:data>" & vbCrLf
TextWriterSchemaRowHeader = schemaString
End function
Function TextWriterSchemaAddRowBegin()
Dim schemaString
schemaString=vbTab & "<z:row "
TextWriterSchemaAddRowBegin=schemaString
End function
Function TextWriterSchemaAddRowFieldNameValue(FieldName, FieldValue)
Dim schemaString
schemaString= FieldName & "='" & FieldValue & "' "
TextWriterSchemaAddRowFieldNameValue=schemaString
End function
Function TextWriterSchemaAddRowEnd()
Dim schemaString
schemaString="/>" & vbCrLf
TextWriterSchemaAddRowEnd=schemaString
End function
Function TextWriterSchemaRowEnd()
Dim schemaString
schemaString=vbTab & "</rs:data>" & vbCrLf
TextWriterSchemaRowEnd=schemaString
End function
Function TextWriterSchemaNameSpaceEnd()
Dim schemaString
schemaString="</xml>" & vbCrLf
TextWriterSchemaNameSpaceEnd=schemaString
End Function
如果我要建立我的XML中,你通過追加字符串有存在的方式,將這項工作罰款或有在MSXML2功能或更高,可以幫助將我的XML字符串轉換爲正確的格式? – Kobojunkie 2010-10-27 01:43:28
您也可以使用MSXML API來構建DOMDocument,並將該參數用於Recordset的Open方法,因爲它可以使用任何實現IXMLDOMDocument所做的IStream接口的對象。請參閱http://support.microsoft.com/kb/263247 – Garett 2010-10-27 04:16:07
我確實嘗試了這種方法,通過objXMLDoc2 = CreateObject(「MSXML2.DOMDocument.3.0」)傳遞到相同的問題。 – Kobojunkie 2010-10-27 13:15:16