Finns det någon som kan visa mig ett bra exempel på hur man använder XML. Hittade lite info som kanske hjälper: Det finns även ett mer avancerat exempel: http://www.freevbcode.com/ShowCode.Asp?ID=312 Tusen tack, det funka bra men svårare att läsa in.XML to Rs?
1. hur man läser upp ett XML doc till ett Rs.
2. Hur man skriver ett Rs till XML doc
Har tittat igenom pellesoft men inget mat nyttigt
Mycket tacksam för hjälp :-)Sv: XML to Rs?
Public Function SaveRSToXML(ConnectionString As String, _
SQLString As String, FullPath As String) As Boolean
'**************************************************
'PURPOSE: SAVE A RECORDSET TO AN XML FILE USING
'ADO 2.5
'PARAMETERS:
'ConnectionString: Valid Connection String
'SQLString: Valid SQL Statement for Data Source specified
' in ConnectionString
'FullPath: FullPath of XMLFile to write to
'RETURNS: True if Sucessful, false otherwise
'REQUIRES: Installation of and reference to ADO 2.5
'EXAMPLE of SaveRsToXML and LoadRSToXML:
'Dim sConnString As String
'Dim sSQL As String
'Dim oRs As ADODB.Recordset
'Dim iCtr As Integer
'sConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
'Source=C:\MyDb.mdb"
'sSQL = "select * from MyTable"
'SaveRSToXML sConnString, sSQL, "C:\My Documents\MyRs.xml"
'Set oRs = LoadRsFromXML("C:\my documents\MyRS.xml")
'If Not oRs Is Nothing Then
' Do While Not oRs.EOF
' For iCtr = 0 To oRs.Fields.Count - 1
' Debug.Print oRs.Fields(iCtr).Name & " = " _
' & oRs.Fields(iCtr).Value & ";";
' Next
' Debug.Print vbCrLf
' oRs.MoveNext
' Loop
' set oRs = nothing
'End If
'******************************************************
Dim oCn As New ADODB.Connection
Dim oCmd As New ADODB.Command
Dim oRs As ADODB.Recordset
On Error GoTo ErrorHandler:
oCn.ConnectionString = ConnectionString
oCn.Open
Set oCmd.ActiveConnection = oCn
oCmd.CommandText = SQLString
oCmd.CommandType = adCmdText
Set oRs = oCmd.Execute
oRs.Save FullPath, adPersistXML
SaveRSToXML = True
ErrorHandler:
On Error Resume Next
Set oRs = Nothing
Set oCmd = Nothing
If oCn.State <> 0 Then oCn.Close
Set oCn = Nothing
End Function
Public Function LoadRsFromXML(FullPath As String) As _
ADODB.Recordset
'**************************************************
'PURPOSE: LOAD A RECORDSET FROM AN XML FILE USING
'ADO 2.5. THE XML FILE MUST HAVE BEEN SAVED
'USING SAVE METHOD OF RECORDSET OBJECT WITH adPersistXML AD
'SECOND PARAMETER
'PARAMETERS:
'FullPath: FullPath of XMLFile to load
'RETURNS: Reference to a Recordset Object, or Nothing if
' Function fails
'REQUIRES: Installation of and reference to ADO 2.5
'EXAMPLE: See Example for SaveRsToXML
'******************************************************
Dim oRs As New ADODB.Recordset
On Error Resume Next
If Dir(FullPath) = "" Then Exit Function
oRs.Open FullPath, "Provider=MSPersist;", adOpenForwardOnly, _
adLockReadOnly, adCmdFile
If Err.Number = 0 Then
Set LoadRsFromXML = oRs
End If
End Function
Sv: XML to Rs?
Sv: XML to Rs?
Det var nog fel metod för att exportera och importera.
Hade tänkt mig något i denna stil:
<main>
<rs>
<person namn="Gunnar">
<older>101</older>
</person>
</rs>
</main>
Någon som XMLDom, hur läser man in denna fil och kommer åt datan i "c:\Export.xml"
//XML Dom
<code>
Private Sub Create_XML()
Dim objDom As DOMDocument
Dim objRootElem As IXMLDOMElement
Dim objMemberElem As IXMLDOMElement
Dim objMemberRel As IXMLDOMAttribute
Dim objMemberName As IXMLDOMElement
Set objDom = New DOMDocument
' Creates root element
Set objRootElem = objDom.createElement("main")
objDom.appendChild objRootElem
' Creates Member element
Set objMemberElem = objDom.createElement("kund")
objRootElem.appendChild objMemberElem
' Creates Attribute to the Member Element
Set objMemberRel = objDom.createAttribute("kundnr")
objMemberRel.nodeValue = "10K"
objMemberElem.setAttributeNode objMemberRel
' Create element under Member element, and
' gives value "some guy"
Set objMemberName = objDom.createElement("Art")
objMemberElem.appendChild objMemberName
objMemberName.Text = "Art100"
'2
Set objMemberName = objDom.createElement("Art")
objMemberElem.appendChild objMemberName
objMemberName.Text = "Art101"
' Saves XML data to disk.
objDom.Save ("c:\Export.xml")
End Sub
</code>