locked
multiple root elements after delete with gridview RRS feed

  • Question

  • User120353699 posted

    using vb in vwd2005/ASP.Net 2.0

    I have a page that builds and loads xml into a memorystream and then binds and displays the data in a gridview.  If i try to delete a row, I get an error saying that there are multiple root elements. The code is below (xDS and xStream are are declared private shared):

        Protected Sub gvEditOL_Bind()
            xDS = New DataSet
            xStream.Flush()
            xStream.Position = 0
            xDS.ReadXml(xStream)
            gvEditOL.DataSource = xDS
            gvEditOL.DataBind()
        End Sub
    
        Protected Sub gvEditOL_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvEditOL.RowDeleting
            Call gvEditOL_Bind()
            xDS = gvEditOL.DataSource
            xDS.Tables(0).Rows(gvEditOL.Rows(e.RowIndex).DataItemIndex).Delete()
            xDS.WriteXml(xStream)
            Call gvEditOL_Bind()
        End Sub


     

    I also noticed that the size and capacity keep increasing, but using debug.print xds.getmxl().tostring(), there is only my original xml, minus the item that was deleted.  I had gotten this code to work with a disk file, but need to make it a memorystream because of a load balanced server environment that does not allow me to create files dynamically, so replaced xmldocument with memorystream.

    Below is the code i use to build the xml

        Protected Sub MakeOL_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim equipAdapter As New VPNMasterTableAdapters.SRSrvcsEquipDefaultsTableAdapter
            Dim equipTable As VPNMaster.SRSrvcsEquipDefaultsDataTable
            Dim equipRow As VPNMaster.SRSrvcsEquipDefaultsRow
    
            'Get the Selected Order List
            equipTable = equipAdapter.GetSRSrvcsEquipDefaultsByListName(ddlOrderList.SelectedValue.ToString())
    
            xStream = New MemoryStream()
            writeOL = XmlWriter.Create(xStream)
            writeOL.WriteStartDocument()
            writeOL.WriteStartElement("OrderList")
    
     //edited for berevity
    
                writeOL.WriteEndElement()
                writeOL.Close()
    
                gvEditOL.Visible = True
                MakeOLLink.Visible = False
                ExportLink.Visible = True
    
                Call gvEditOL_Bind()
    
            End If
    
            lblMakeOL.Visible = False
            GetCSV.Visible = False
            lblOLSelect.Visible = False
            ddlOrderList.Visible = False
            ExportLink.Visible = True
    
        End Sub
    


     

    Tuesday, August 25, 2009 3:28 PM

Answers

  • User120353699 posted

    See if this helps

    http://kavstech.blogspot.com/2009/06/how-to-remove-nodes-from-xml.html


     

    Thanks for the reply, but I am interested in using the gridview to do the delete (as is happens, even editing a row causes the same issues with the memorystream).  The xml as memorystream is already bound to the gridview as a dataset.  I'm just curious why what is humanly readable and well formed xml is causing a problem and how to fix it so that it can be rebound after the deletion (or edit as the case seems to be).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 26, 2009 8:45 AM

All replies

  • User-1293249277 posted

    See if this helps

    http://kavstech.blogspot.com/2009/06/how-to-remove-nodes-from-xml.html


    Wednesday, August 26, 2009 6:08 AM
  • User1835330922 posted

    Your gvEditOL_RowDeleting procedure calls gvEditOL_Bind which reads from the memory stream so then the memory stream is positioned at the very end of the XML document. Then you do a WriteXml to that stream which means you add the markup of a second XML document to the memory stream. Then you call gvEditOL_Bind again setting the Position of the memory stream to 0 and trying to read from the stream. That fails as the stream has the markup of two XML documents (two root elements) which is not allowed. So before writing to the memory stream you need to set Position to 0 to ensure you overwrite the contents.

    Wednesday, August 26, 2009 7:51 AM
  • User120353699 posted

    See if this helps

    http://kavstech.blogspot.com/2009/06/how-to-remove-nodes-from-xml.html


     

    Thanks for the reply, but I am interested in using the gridview to do the delete (as is happens, even editing a row causes the same issues with the memorystream).  The xml as memorystream is already bound to the gridview as a dataset.  I'm just curious why what is humanly readable and well formed xml is causing a problem and how to fix it so that it can be rebound after the deletion (or edit as the case seems to be).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 26, 2009 8:45 AM
  • User120353699 posted

    Your gvEditOL_RowDeleting procedure calls gvEditOL_Bind which reads from the memory stream so then the memory stream is positioned at the very end of the XML document. Then you do a WriteXml to that stream which means you add the markup of a second XML document to the memory stream. Then you call gvEditOL_Bind again setting the Position of the memory stream to 0 and trying to read from the stream. That fails as the stream has the markup of two XML documents (two root elements) which is not allowed. So before writing to the memory stream you need to set Position to 0 to ensure you overwrite the contents.

     

     

    I really don't know why that wasn't obvious to me...so I now have xstream.position()=0 before the dataset writes back to the memorystream and all is good...

     

    Thanks for the help!

    Wednesday, August 26, 2009 9:55 AM