I wrote a Class to write an XML file according to user input. I have a button called "Add Rule", the user will be adding 'Rules' to the XML file using the same button.
The code:
Imports System.Xml
Public Class KEClass
'Declarations
'*****************************************************************
'Counter for the loop that will write the symptoms to the xml file
'*****************************************************************
Dim counter As Integer = 0
'*******************
'Counter for rule #
'*******************
Dim ruleNo As Integer = 1
'*************************
'Creating the xml document
'*************************
Dim writer As New XmlTextWriter(Application.StartupPath + "\New-Definition.xml", Nothing)
Private Sub writeStartElements()
'Start Document ( Write the beginning tags )
writer.WriteStartDocument()
'Write root elements
writer.WriteStartElement("rules")
writer.WriteStartElement("rule")
'Write attributes to rule
writer.WriteAttributeString("id", ruleNo)
'Write string to rule ( content within the rule tags )
writer.WriteAttributeString("specificity", "140")
End Sub
Private Function getSymptomTypeValue(ByRef datagrid)
Return datagrid.Rows(counter).Cells(1).Value()
End Function
Private Function getSymptomName(ByRef datagrid)
Return datagrid.Rows(counter).Cells(0).Value
End Function
Private Sub createSymptoms(ByRef datagrid)
writer.WriteStartElement("antecedent")
writer.WriteAttributeString("type", getSymptomTypeValue(datagrid))
writer.WriteString(getSymptomName(datagrid))
writer.WriteEndElement()
End Sub
'**************************************************
'Function to write all data to the created xml file
'**************************************************
Public Sub createFile(ByRef datagrid As Object, ByRef rowcount As Integer, ByRef consequentType As String)
writeStartElements()
While (counter < rowcount - 1)
createSymptoms(datagrid)
counter = counter + 1
ruleNo = ruleNo + 1
End While
writer.Close()
End Sub
End Class
Main function is 'createFile'. My problem is that after the user adds the 1st rule, he will attempt to add the 2nd rule which will overwrite the XML file. ( In other words the XML file will contain only the LAST rule the user has added )
I think the problem is because every time the user pushes the button he will be dimming a new 'XmlTextWriter' which will overwrite the old file.. I tried removing 'writer.close()' but I got an error that the file is already in use and can't be accessed.
Sorry for my ignorance, I'm pretty new to VB.NET and I'm learning, hope you understand the problem and someone out there can help me..
Question
murderdoll
Hello,
I wrote a Class to write an XML file according to user input. I have a button called "Add Rule", the user will be adding 'Rules' to the XML file using the same button.
The code:
Imports System.Xml Public Class KEClass 'Declarations '***************************************************************** 'Counter for the loop that will write the symptoms to the xml file '***************************************************************** Dim counter As Integer = 0 '******************* 'Counter for rule # '******************* Dim ruleNo As Integer = 1 '************************* 'Creating the xml document '************************* Dim writer As New XmlTextWriter(Application.StartupPath + "\New-Definition.xml", Nothing) Private Sub writeStartElements() 'Start Document ( Write the beginning tags ) writer.WriteStartDocument() 'Write root elements writer.WriteStartElement("rules") writer.WriteStartElement("rule") 'Write attributes to rule writer.WriteAttributeString("id", ruleNo) 'Write string to rule ( content within the rule tags ) writer.WriteAttributeString("specificity", "140") End Sub Private Function getSymptomTypeValue(ByRef datagrid) Return datagrid.Rows(counter).Cells(1).Value() End Function Private Function getSymptomName(ByRef datagrid) Return datagrid.Rows(counter).Cells(0).Value End Function Private Sub createSymptoms(ByRef datagrid) writer.WriteStartElement("antecedent") writer.WriteAttributeString("type", getSymptomTypeValue(datagrid)) writer.WriteString(getSymptomName(datagrid)) writer.WriteEndElement() End Sub '************************************************** 'Function to write all data to the created xml file '************************************************** Public Sub createFile(ByRef datagrid As Object, ByRef rowcount As Integer, ByRef consequentType As String) writeStartElements() While (counter < rowcount - 1) createSymptoms(datagrid) counter = counter + 1 ruleNo = ruleNo + 1 End While writer.Close() End Sub End ClassMain function is 'createFile'. My problem is that after the user adds the 1st rule, he will attempt to add the 2nd rule which will overwrite the XML file. ( In other words the XML file will contain only the LAST rule the user has added )
I think the problem is because every time the user pushes the button he will be dimming a new 'XmlTextWriter' which will overwrite the old file.. I tried removing 'writer.close()' but I got an error that the file is already in use and can't be accessed.
Sorry for my ignorance, I'm pretty new to VB.NET and I'm learning, hope you understand the problem and someone out there can help me..
Cheers,
Edited by murderdollLink to comment
Share on other sites
1 answer to this question
Recommended Posts