Results 1 to 3 of 3

Thread: Appending to XML File

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Appending to XML File

    Hello, Alright. I'm having trouble appending my Data to a XML File.

    First, I'm creating the XML File Here.

    vb Code:
    1. Public Sub xmlCreateAccount(ByVal ID As String, ByVal Username As String, ByVal Password As String, ByVal GUID As String, ByVal FirstName As String, ByVal LastName As String, ByVal BirthDate As String, ByVal PhoneNumber As String, ByVal xmlWriter As XmlTextWriter)
    2.         With xmlWriter
    3.             .WriteStartElement("Account")
    4.             .WriteStartElement("AccountID")
    5.             .WriteString(ID)
    6.             .WriteEndElement()
    7.             .WriteStartElement("Username")
    8.             .WriteString(Username)
    9.             .WriteEndElement()
    10.             .WriteStartElement("Password")
    11.             .WriteString(Password)
    12.             .WriteEndElement()
    13.             .WriteStartElement("GUID")
    14.             .WriteString(GUID)
    15.             .WriteEndElement()
    16.             .WriteStartElement("FirstName")
    17.             .WriteString(FirstName)
    18.             .WriteEndElement()
    19.             .WriteStartElement("LastName")
    20.             .WriteString(LastName)
    21.             .WriteEndElement()
    22.             .WriteStartElement("BirthDate")
    23.             .WriteString(BirthDate)
    24.             .WriteEndElement()
    25.             .WriteStartElement("PhoneNumber")
    26.             .WriteString(PhoneNumber)
    27.             .WriteEndElement()
    28.             .WriteEndElement()
    29.         End With
    30.     End Sub

    Then, I call it here:

    vb Code:
    1. Private Sub btnRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegister.Click
    2.         If IO.File.Exists(DataPath & "Accounts.xml") Then
    3.             Dim xmlDoc As New Xml.XmlDocument()
    4.             xmlDoc.Load(DataPath & "Accounts.xml")
    5.         Else
    6.             Dim xmlWriter As New Xml.XmlTextWriter(DataPath & "Accounts.xml", System.Text.Encoding.UTF8)
    7.             Clipboard.SetText(sGUIDCode)
    8.             xmlWriter.WriteStartDocument(True)
    9.             xmlWriter.Formatting = Xml.Formatting.Indented
    10.             xmlWriter.Indentation = 5
    11.             xmlWriter.WriteStartElement("Accounts")
    12.             xmlCreateAccount(1, txtUsername.Text, Crypt(txtPassword.Text), Crypt(sGUIDCode), txtFirstName.Text, txtLastName.Text, dtpBirthDate.Value.ToShortDateString, txtPhoneNumber.Text, xmlWriter) 'CALLED HERE
    13.             xmlWriter.WriteEndElement()
    14.             xmlWriter.WriteEndDocument()
    15.             xmlWriter.Close()
    16.         End If
    17.         frmGUID.ShowDialog()
    18.         Me.Close()
    19.     End Sub

    So now. How do I append another xmlCreateAccount(....) to my Already Made XML File without writing over whats already their? But I also want it to WRITE under the last xmlCreateAccount and Above the EndElement("Accounts")

  2. #2
    Member GeekInOhio's Avatar
    Join Date
    Jun 2008
    Location
    You'll never guess...
    Posts
    56

    Re: Appending to XML File

    VB.Net offers a much easier way to deal with XML files.

    Use this sub in place of your xmlCreateAccount sub:
    vb Code:
    1. Public Sub xmlAccountWriter(ByVal ID As String, ByVal Username As String, ByVal Password As String, ByVal GUID As String, ByVal FirstName As String, ByVal LastName As String, ByVal BirthDate As String, ByVal PhoneNumber As String)
    2.  
    3.         'The path and filename where you want to store this account XML file
    4.         Dim myPath As String = "C:\accounts.xml" ' << Change this
    5.  
    6.         'First, check to see if we have an existing XML file
    7.         If IO.File.Exists(myPath) = False Then
    8.  
    9.             'It doesn't exists, so we'll create a new file.
    10.             'First, create a table to store our values in.
    11.             Dim gridtable As DataTable = New DataTable("Account Info")
    12.  
    13.             Dim gridtable_column0 As DataColumn = New DataColumn("Username")
    14.             Dim gridtable_column1 As DataColumn = New DataColumn("Password")
    15.             Dim gridtable_column2 As DataColumn = New DataColumn("GUID")
    16.             Dim gridtable_column3 As DataColumn = New DataColumn("FirstName")
    17.             Dim gridtable_column4 As DataColumn = New DataColumn("LastName")
    18.             Dim gridtable_column5 As DataColumn = New DataColumn("BirthDate")
    19.             Dim gridtable_column6 As DataColumn = New DataColumn("PhoneNumber")
    20.  
    21.             gridtable.Columns.Add(gridtable_column0)
    22.             gridtable.Columns.Add(gridtable_column1)
    23.             gridtable.Columns.Add(gridtable_column2)
    24.             gridtable.Columns.Add(gridtable_column3)
    25.             gridtable.Columns.Add(gridtable_column4)
    26.             gridtable.Columns.Add(gridtable_column5)
    27.             gridtable.Columns.Add(gridtable_column6)
    28.  
    29.             'Now add the first row to this table, with the data we were supplied
    30.  
    31.             Dim table_row As DataRow
    32.  
    33.             table_row = gridtable.NewRow
    34.             table_row("Username") = Username
    35.             table_row("Password") = Password
    36.             table_row("GUID") = GUID
    37.             table_row("FirstName") = FirstName
    38.             table_row("LastName") = LastName
    39.             table_row("BirthDate") = BirthDate
    40.             table_row("PhoneNumber") = PhoneNumber
    41.  
    42.             'Add the row
    43.             gridtable.Rows.Add(table_row)
    44.  
    45.             'Now write it as an XML file to our path that we picked above
    46.             gridtable.WriteXml(myPath)
    47.  
    48.         Else
    49.  
    50.             'If we are here, then there is a pre-existing XML file.  We'll add onto it.
    51.  
    52.             Dim myXMLDataSet As New DataSet
    53.  
    54.             'Read in the existing XML File
    55.             myDataSet.ReadXml(myPath)
    56.  
    57.             'Create a new row of information to add to it
    58.             Dim table_row As DataRow
    59.  
    60.             table_row = myDataSet.Tables(0).NewRow
    61.             table_row("Username") = Username
    62.             table_row("Password") = Password
    63.             table_row("GUID") = GUID
    64.             table_row("FirstName") = FirstName
    65.             table_row("LastName") = LastName
    66.             table_row("BirthDate") = BirthDate
    67.             table_row("PhoneNumber") = PhoneNumber
    68.  
    69.             'Add the row
    70.             myDataSet.Tables(0).Rows.Add(table_row)
    71.  
    72.             'Now write it as an XML file to our path that we picked above
    73.             myDataSet.WriteXml(myPath)
    74.  
    75.         End If
    76.  
    77.     End Sub

    Call it like so:
    vb Code:
    1. Private Sub btnRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegister.Click
    2.         xmlAccountWriter(ID, Username, Password, GUID, FirstName, LastName, BirthDate, PhoneNumber)
    3.     End Sub

    This will create a new XML file if one does not exist. Otherwise, it will append a new entry onto the existing XML file. Using similiar logic, you can also edit and delete existing entries in that file.

    Hope that helps.
    In case I forget: I'm using Visual Basic 2008 Express Edition...

    Should I, in my odd, bumbling way, actually offer some assistance, feel free to show me some RATE love.


    Just Another Laptop Hero

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Appending to XML File

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width