Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update an XML file through File I/O

Status
Not open for further replies.

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:

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.
 
I'm interested to know why you want to do this without using any 'external objects'?
 
strongm, The answer may lie here:
thread222-668110

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
My application must be used in a local area network, so I can't implement any setup procedure.

The lack of external objects allows my VB5 to work as a standalone executable.
 
Isn't this the third time this question has been asked?

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
If it is true that I may have asked this question three times, it is also true that so far I haven't received any answer that could sort out my problem.

Any help will be greatly appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top