How to update or delete a record from an XML database

esposito

Centurion
Joined
Jul 11, 2003
Messages
103
Location
Perugia - Italy
Hello, I need your help. I have created a very simple XML file, called "news.xml", structured in the following way:

Code:
<News_Database>

   <My_News>
      <news1>First News A</news1>
      <news2>Second News A</news2>
   </My_News>

   <My_News>
      <news1>First News B</news1>
      <news2>Second News B</news2>
   </My_News>

   <My_News>
      <news1>First News C</news1>
      <news2>Second News C</news2>
   </My_News>

</News_Database>

In order to populate the XML file above, I use the following code:

Visual Basic:
Sub btnSaveRecord_Click(sender As Object, e As EventArgs)

  Try

    Dim objDataSet As New System.Data.DataSet

    objDataSet.ReadXml("news.xml")

    Dim News1_Var As String
    Dim News2_Var As String

    News1_Var = txtNews1.Text
    News2_Var = txtNews2.Text

    Dim v(1) As String
    v(0) = News1_Var
    v(1) = News2_Var

    objDataSet.Tables(0).Rows.Add(v)

    objDataSet.WriteXml("news.xml")

        Dim count As Integer

        objDataSet.ReadXml(Server.MapPath("news.xml"), XmlReadMode.Auto)

        count = objDataSet.Tables(0).Rows.Count

        lblRecordNumber.Text = count / 2

    Catch ex As Exception
        Exit Sub
    End Try

End Sub

As you can see, I have used a Label (lblRecordNumber) to keep track of the current record number. This number could be referred to as an ID when you want to search for a record.

Now, my question is, is there any way I can update or delete a record from the database?

TIA
 
I have managed to update an existing record in the database by adding an ID field to the XML file, which now looks as follows:
Code:
<News_Database>

   <My_News>
      <ID>1</ID>
      <news1>First News A</news1>
      <news2>Second News A</news2>
   </My_News>

   <My_News>
      <ID>2</ID>
      <news1>First News B</news1>
      <news2>Second News B</news2>
   </My_News>

   <My_News>
      <ID>3</ID>
      <news1>First News C</news1>
      <news2>Second News C</news2>
   </My_News>

</News_Database>
This is the code I have used to update the record.
Code:
Sub btnUpdateRecord_Click(sender As Object, e As EventArgs)

        Dim objDataSet As New System.Data.DataSet
        Dim count As Integer

        objDataSet.ReadXml(Server.MapPath("news.xml"), XmlReadMode.Auto)
        count = objDataSet.Tables(0).Rows.Count

        Dim i As Integer

        For i = 0 to count - 1

        If objDataSet.Tables(0).Rows(i).Item("ID") = txtID.Text Then
            objDataSet.Tables(0).Rows(i).Item("news1") = txtNews1.Text
            objDataSet.Tables(0).Rows(i).Item("news2") = txtNews2.Text
            objDataSet.WriteXml("news.xml")
        End If

        Next

End Sub
I would very much like to know if and how it is possible to DELETE a record from the database, if possible modifying the code above.

Any help will be greatly appreciated.
 
Back
Top