1

我在将数据呈现到 XML 文件的提交表单(文本框和下拉控件)中使用 GridView(插入/编辑/删除)。我可以通过 GridView 编辑/插入/删除 XML 文件,并且表单控件标签也可以在单击提交问题时成功地将输入的数据复制到 XML 中。我的问题是,当单击提交按钮时,来自 Gridview 的数据被删除,因此只留下从表单控制标签提交的数据。

以下是用于编写通过表单控制标签提交的数据的代码(按钮事件处理程序):

Public Sub Write_XML(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim textWriter As New XmlTextWriter(Server.MapPath("xml/PersonDataVI.xml"), Nothing)
        textWriter.Formatting = System.Xml.Formatting.Indented
        'Start New Document
        textWriter.WriteStartDocument()
        'Write a Comment
        textWriter.WriteComment("This is a comment")
        'Insert Start Element -root element node
        textWriter.WriteStartElement("PersonalData")

        'write the child element
        textWriter.WriteStartElement("products")

        'write the child element
        textWriter.WriteStartElement("product")

        'save the data for the Grid
        Dim productID As String = DirectCast(gvProductNumber.FooterRow.FindControl("txtProductIDInsert"), 

TextBox).Text
        Dim productNumber As String = DirectCast(gvProductNumber.FooterRow.FindControl

("txtProductNumberInsert"), TextBox).Text
        BindGridView()
        Dim dtXMLInsert As DataTable = DirectCast(gvProductNumber.DataSource, DataTable)
        Dim dsgvProductNumber As New DataSet()
        dsgvProductNumber.ReadXml(Server.MapPath("~/xml/PersonDataVI.xml"))
        Dim drInsert As DataRow = dsgvProductNumber.Tables("product").NewRow()
        drInsert("productID") = productID
        drInsert("productNumber") = productNumber
        Dim drParent As DataRow = dsgvProductNumber.Tables("products").Rows(0)
        drInsert.SetParentRow(drParent)
        dsgvProductNumber.Tables("product").Rows.Add(drInsert)
        dsgvProductNumber.WriteXml(Server.MapPath("~/xml/PersonDataVI.xml"))
        BindGridView()

        textWriter.WriteEndElement()
        textWriter.WriteEndElement()

        'write the child element
        textWriter.WriteStartElement("customers")

        'write the child element
        textWriter.WriteStartElement("customer")

        'Write LastName Element and Data
        textWriter.WriteStartElement("LastName", "")
        textWriter.WriteString(txtLastName.Text)
        textWriter.WriteEndElement()

        'Write FirstName Element and Data
        textWriter.WriteStartElement("FirstName", "")
        textWriter.WriteString(txtFirstName.Text)
        textWriter.WriteEndElement()

        'Write Address Element and Data
        textWriter.WriteStartElement("Address", "")
        textWriter.WriteString(txtAddress.Text)
        textWriter.WriteEndElement()

        'Write City Element and Data
        textWriter.WriteStartElement("City", "")
        textWriter.WriteString(txtCity.Text)
        textWriter.WriteEndElement()

        'Write State Element and Data
        textWriter.WriteStartElement("State", "")
        textWriter.WriteString(txtState.Text)
        textWriter.WriteEndElement()

        'Write ZipCode Elment and Data
        textWriter.WriteStartElement("ZipCode", "")
        textWriter.WriteString(txtZipCode.Text)
        textWriter.WriteEndElement()

        'End Everything
        textWriter.WriteEndDocument()

        'Clean up
        textWriter.Flush()
        textWriter.Close()

        'Display the XML Document
        Response.Redirect(Server.MapPath("xml/PersonDataVI.xml"))
    End Sub

...该代码适用于将通过表单控制标签输入的数据复制到 xml 文件中。我的问题是,当单击按钮时,如何从 Gridview 获取数据以保留在 XML 文件中。正如上一篇文章所建议的那样,(请在我的代码片段中添加注释以保存 Gridview 数据)我添加了一个新的 dataTable 并向其中添加了一些列,并用一些数据行填充它,并将其添加到按钮事件处理程序中。但是,现在我收到“xml 文件正在被另一个应用程序使用”的错误消息。我的 xml 文件现在只是空白。您能否看一下我上面提供的代码段,并就我做错了什么提供一些指导?

4

1 回答 1

0

这里有两个问题 - 1.每次保存xml时,都会覆盖xml文件,因此以前的数据丢失,2.使用xmltextwriter打开并保存xml文件后,释放文件需要一点时间,您可以在暂停后尝试打开它。我更喜欢使用 xdocument。请查看这篇文章:将 XML 写入文件而不覆盖以前的数据

编辑:您可以通过以下方式获得所需的结果:

Public Sub Write_XML(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'You have saved xml, now want to add data thorugh text boxes

    Dim dsgvPersonalData As New DataSet()
    dsgvPersonalData.ReadXml(Server.MapPath("~/xml/PersonDataVI.xml"))

    'Add new Product
    Dim drNewProduct As DataRow = dsgvPersonalData.Tables("products").NewRow()
    Dim productID As String = DirectCast(gvProductNumber.FooterRow.FindControl("txtProductIDInsert"), TextBox).Text
    Dim productNumber As String = DirectCast(gvProductNumber.FooterRow.FindControl("txtProductNumberInsert"), TextBox).Text
    drNewProduct("productID") = productID
    drNewProduct("productID") = productNumber
    dsgvPersonalData.Tables("products").Rows.Add(drNewProduct)

    'Add new Customer
    Dim drNewCustomer As DataRow = dsgvPersonalData.Tables("customesr").NewRow()
    drNewProduct("LastName") = txtLastName.Text
    drNewProduct("FirstName") = txtFirstName.Text
    drNewProduct("Address") = txtAddress.Text
    drNewProduct("City") = txtCity.Text
    drNewProduct("State") = txtState.Text
    drNewProduct("ZipCode") = txtZipCode.Text
    dsgvPersonalData.Tables("customerx").Rows.Add(drNewProduct)

    'Save the xml
    dsgvPersonalData.WriteXml(Server.MapPath("~/xml/PersonDataVI.xml"))

    'databinding is easy here
    gvProductNumber.DataSource = dsgvPersonalData.Tables("products")
    gvProductNumber.DataBind()
End Sub
于 2013-08-17T06:56:51.373 回答