Log in

View Full Version : Writing and reading to an XML file


shaxs
06-19-2005, 02:30 AM
Hello all,


I am trying to create a very simple form to take market survey questions in down town San Diego. Basically, I need the program to read the current value in an XML file and +=1 if a specific radial button is selected. The below code is what I currently have. I know it is horribly wrong. I am not much a programmer, just an ideas guy. I usually have to pay people to develop applications for me, but for such a small application, I was hoping someone here could help me. The code below compiles without erros, but crashes the program when run. Also, It just writes the whole number to a file. I guess this works as is, but it would be better if it read the current value and just added one to it. If you can help, I'd be grateful! thanks!!
===============================
Public Class Form1
Inherits System.Windows.Forms.Form
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Dim mintSurveyCounter As Integer = 0
Dim mintAge1521 As Integer = 0
Dim mintAge2230 As Integer = 0
Dim mintAge3140 As Integer = 0
Dim mintAge41 As Integer = 0
Dim filename As String = "Survey"

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
mintSurveyCounter += 1
lblNumberSurveyed.Text = CStr(mintSurveyCounter)
If radAge1521.Checked = True Then
mintAge1521 += 1
End If
If radAge2230.Checked = True Then
mintAge2230 += 1
End If
If radAge3140.Checked = True Then
mintAge3140 += 1
End If


Dim writer As Xml.XmlTextWriter = New Xml.XmlTextWriter(filename, System.Text.Encoding.ASCII)


writer.WriteStartDocument()
writer.WriteStartElement("survey")
writer.WriteAttributeString("15to21", "", mintAge1521)
writer.WriteAttributeString("22to30", "", mintAge2230)
writer.WriteAttributeString("31to40", "", mintAge3140)
writer.WriteAttributeString("41plus", "", mintAge41)

writer.WriteEndElement()
writer.WriteEndDocument()
writer.Close()
End Sub