views:

9

answers:

0

I have an asp.net 2.0 (VB) application that loads an xml file into a datagrid. From there I can edit, update, delete and add new items in a web page. The problem I have is my XML file isn’t RSS compliant. If I add an extra node to my XML file then the form will no longer allow me to add new items however I can still edit and delete items. How do I adapt my code to accommodate the new xml file? I’d like to be able to import the new format, be able to edit, add and delete items. I’m assuming it just a matter of making a tweak to the btnUpdate – but I’m stumped to find where?

Here’s my vb and my xml

Old xml

<?xml version="1.0" standalone="yes"?>  
<channel>  
<item>  
  <title>Keep checking here for updated clinic running times</title>  
  <description>titme date here</description>  
</item>  
<item>  
  <title>running times</title>  
  <description>time date here</description>  
</item>  
<item>  
  <title>Keep checking</title>  
  <description>time date here</description>  
</item>  
<item>  
  <title>Keep on running</title>  
  <description>time date here</description>   
</item>  
</channel> 

And the new xml

<?xml version="1.0" standalone="yes"?>  
<rss version="2.0">  
<channel>  
<item>  
  <title>Keep checking here for updated clinic running times</title>  
  <description>Static1</description>  
</item>  
<item>  
  <title>running times</title>  
  <description>date time here</description>  
</item>  
<item>  
  <title>Keep checking</title> 
  <description> date time here </description>  
</item>  
<item>  
  <title>Keep on running</title>  
  <description> date time here </description>  
</item>  
 </channel>  
</rss>

And finally my VB

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)     Handles MyBase.Load   

    ' Load the XML file.   

    _dsTicker = New DataSet   

    _dsTicker.ReadXml(Server.MapPath("ticker.xml"))   

    Dim dcPk(0) As DataColumn   

    dcPk(0) = _dsTicker.Tables("item").Columns("title")   

    _dsTicker.Tables("item").PrimaryKey = dcPk   



    If Not Page.IsPostBack Then  

        'Only bind at this point if this is the first page request.   

        BindTicker()   

        _sSort = "title"  

    Else  

        ' Read the sort order from the view state.   

        _sSort = CType(ViewState("Sort"), String)   

    End If  



 End Sub  



 Private Sub BindTicker()   

    ' Save the sort order to the view state.   

    ViewState("Sort") = _sSort   



    ' Bind the grid to the sorted data view.   

    Dim dv As DataView = New DataView(_dsTicker.Tables("item"))   

    dv.Sort = _sSort   

    dgTicker.DataSource = dv   

    dgTicker.DataBind()   

  End Sub  



  Private Sub SaveTicker()   

    _dsTicker.WriteXml(Server.MapPath("ticker.xml"))   

  End Sub  



  Private Sub dgTicker_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgTicker.ItemCommand   

    If e.CommandName = "Add" Then  

        'Triggers a reminder to click update   

        DontForget.Visible = True  



        ' Add the new item to the dataset.  I use an array here for efficiency.   



        Dim sitem(1) As String  



        sitem(0) = CType(e.Item.FindControl("NewDescription"), TextBox).Text   

        'Adds time and date to show when message added   

        sitem(0) = Now()   



        sitem(1) = CType(e.Item.FindControl("NewTitle"), TextBox).Text   

        'Puts current time and date if user tries to add a blank message   

        If sitem(1) = "" Then  

            sitem(1) = Now()   

        Else  

            'Adds the new message   

            sitem(1) = CType(e.Item.FindControl("NewTitle"), TextBox).Text   

        End If  



        _dsTicker.Tables("item").Rows.Add(sitem)   

        SaveTicker()   

        BindTicker()   

    End If  

  End Sub  



  Private Sub dgTicker_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgTicker.SortCommand   

    ' If we're already sorting by this column, change it to descending otherwise change the sort.   

     If e.SortExpression = _sSort Then  

        _sSort = _sSort & " DESC"  

    Else  

        _sSort = e.SortExpression   

    End If  

    BindTicker()   

   End Sub  



   Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click   



    Dim di As DataGridItem   

    'Hides the rteminder if it's visible   

    DontForget.Visible = False  



    ' Loop through the items in the datagrid.   

    For Each di In dgTicker.Items   



        ' Make sure this is an item and not the header or footer.   

        If di.ItemType = ListItemType.Item OrElse di.ItemType = ListItemType.AlternatingItem Then  

            ' Get the current row for update or delete operations later.   

            Dim dr As DataRow = _dsTicker.Tables("item").Rows.Find(dgTicker.DataKeys(di.ItemIndex))   



            ' See if this one needs to be deleted.   

            If CType(di.FindControl("chkDelete"), CheckBox).Checked Then  

                _dsTicker.Tables("item").Rows.Remove(dr)   

            Else  

                ' Update the row instead.   

                dr("title") = CType(di.FindControl("title"), TextBox).Text   

                dr("description") = CType(di.FindControl("description"), TextBox).Text   



            End If  

        End If  

       Next  



    ' Save the changes if there are any.   

    If _dsTicker.HasChanges Then  

        SaveTicker()   

    End If  

    BindTicker()   

   End Sub