espositophp
Programmer
- Sep 30, 2003
- 31
Hello, I have a problem dealing with File I/O.
Thanks to the help of this forum, I have been able to create a VB5 application that allows you to retrieve data from the fields of an XML file through pure VB code, i.e. without referring to any external objects.
The application I'm talking about consists of two textboxes (txtXmlField1 and txtXmlField2), a command button (cmdReadFromXML) and a label (lblOccurrence).
Clicking on cmdReadFromXML allows the user to read the records from an XML file (myXml.xml) one at a time. The XML file is located in the same folder as the application and has a very simple structure which is the following:
To achieve the above mentioned objective, we had to create a function that could detect the delimiters of the fields in the XML file to read from. That function runs as follows:
And this is the code we placed in the command button to use the function:
Now, I was wondering whether it is possible to add a second command button (cmdUpdateXML) to update a record from VB, once you have shown it in the textboxes, taking advantage of the fact that its position in the XML file can be read from the label.
Shortly speaking, I would like the user to be able to change the content of the current record in the textboxes and overwrite the old data with the new one in the XML file.
Any help will be greatly appreciated.
Thanks to the help of this forum, I have been able to create a VB5 application that allows you to retrieve data from the fields of an XML file through pure VB code, i.e. without referring to any external objects.
The application I'm talking about consists of two textboxes (txtXmlField1 and txtXmlField2), a command button (cmdReadFromXML) and a label (lblOccurrence).
Clicking on cmdReadFromXML allows the user to read the records from an XML file (myXml.xml) one at a time. The XML file is located in the same folder as the application and has a very simple structure which is the following:
Code:
<?xml version='1.0' encoding='ISO8859-1' ?>
<mydatabase>
<myrecord>
<myfield1>First occurrence of myfield1</myfield1>
<myfield2>First occurrence of myfield2</myfield2>
<myfield3>First occurrence of myfield3</myfield3>
</myrecord>
<myrecord>
<myfield1>Second occurrence of myfield1</myfield1>
<myfield2>Second occurrence of myfield2</myfield2>
<myfield3>Second occurrence of myfield3</myfield3>
</myrecord>
<myrecord>
<myfield1>Third occurrence of myfield1</myfield1>
<myfield2>Third occurrence of myfield2</myfield2>
<myfield3>Third occurrence of myfield3</myfield3>
</myrecord>
</mydatabase>
To achieve the above mentioned objective, we had to create a function that could detect the delimiters of the fields in the XML file to read from. That function runs as follows:
Code:
Private Function InString(SourceLine As String, StartSource As String, EndSource As String, Seq As Integer)
Dim i As Long
Dim j As Long
Dim TempStr As String
Dim z As Integer
i = 1
For z = Seq To 0 Step -1
i = InStr(i, SourceLine, StartSource)
If i <> 0 Then
i = i + Len(StartSource)
j = InStr(i, SourceLine, EndSource)
If j <> 0 And z = 1 Then
TempStr = Mid$(SourceLine, i, j - i)
InString = TempStr
Exit For
End If
Else
Exit For
End If
Next z
End Function
And this is the code we placed in the command button to use the function:
Code:
Private Sub cmdReadFromXML_Click()
Dim StrXML As String
Dim ff As Long
ff = FreeFile
Open App.Path & "\MyXML.xml" For Binary As #ff
StrXML = Input$(LOF(1), 1)
lblOccurrence.Caption = Val(lblOccurrence.Caption) + 1
Dim myOccurrence As Integer
myOccurrence = Val(lblOccurrence.Caption)
If InString(StrXML, "<myfield1>", "</myfield1>", myOccurrence) = False Then
lblOccurrence.Caption = Val(lblOccurrence.Caption) - 1
myOccurrence = Val(lblOccurrence.Caption)
txtXmlField1.Text = InString(StrXML, "<myfield1>", "</myfield1>", myOccurrence)
txtXmlField2.Text = InString(StrXML, "<myfield2>", "</myfield2>", myOccurrence)
Close #ff
MsgBox "This is the last record in the database."
Exit Sub
End If
txtXmlField1.Text = InString(StrXML, "<myfield1>", "</myfield1>", myOccurrence)
txtXmlField2.Text = InString(StrXML, "<myfield2>", "</myfield2>", myOccurrence)
Close #ff
End Sub
Now, I was wondering whether it is possible to add a second command button (cmdUpdateXML) to update a record from VB, once you have shown it in the textboxes, taking advantage of the fact that its position in the XML file can be read from the label.
Shortly speaking, I would like the user to be able to change the content of the current record in the textboxes and overwrite the old data with the new one in the XML file.
Any help will be greatly appreciated.