【发布时间】:2015-02-22 10:50:06
【问题描述】:
尝试编写一些代码以通过用户界面更改 XML 文件中的元素顺序。问题是:如何控制通过WriteXML写入的物理存储顺序元素
比如我要改造:
<Image Name="Middle" file="Sauce.png" />
<Image Name="Top" file="Cheese.png" />
<Image Name="Bottom" file="PizzaBase.png" />
到:
<Image Name="Top" file="Cheese.png" />
<Image Name="Middle" file="Sauce.png" />
<Image Name="Bottom" file="PizzaBase.png" />
在文件中。到目前为止的方法:
.xml > 数据集> datagridview(绑定dataset.table)
这很好用,允许用户上下移动行/元素。当我在 Locals 中查看这一行时,行的任何移动都会反映在 Dataset.table 中(即,如果“Cheese”移动到 DatagridView 的顶部,它也位于数据集中的顶部)。
但是,当我通过 XMLWriter 将其写回 XML 时,任何移动的行都会附加到 xml 的 底部,而不是按照数据集的相同顺序。例如,如果我将“Cheese”向上移动,则输出为:
<Image Name="Middle" file="Sauce.png" />
<Image Name="Bottom" file="PizzaBase.png" />
<Image Name="Top" file="Cheese.png" />
四处搜索 - 找不到任何关于此的内容。有人有什么想法吗?
编辑:这就是我重新排序行的方式:
Private Sub _RigRowUp_Click(sender As Object, e As EventArgs) Handles _RigRowUp.Click
If IsNothing(_RigImgsDGV.DataSource)
Return
End If
Dim selectedRow As DataRow = _RigXMLImgsDS.Tables("Image").Rows(_RigImgsDGV.CurrentRow.Index)
Dim selectedindex As Integer = _RigXMLImgsDS.Tables("Image").Rows.IndexOf(selectedRow)
If selectedindex <= 0 Then
Return
End If
Dim newRow As DataRow = _RigXMLImgsDS.Tables("Image").NewRow()
newRow.ItemArray = selectedRow.ItemArray
_RigXMLImgsDS.Tables("Image").Rows.Remove(selectedRow)
_RigXMLImgsDS.Tables("Image").Rows.InsertAt(newRow, selectedindex - 1)
_RigImgsDGV.ClearSelection()
_RigImgsDGV.CurrentCell = _RigImgsDGV.Rows(selectedindex - 1).Cells(0)
_RigImgsDGV.Rows(selectedindex - 1).Selected = True
End Sub
_RigXMLImgsDS 是数据集 RigImgsDGV 是 DataGridView
通过以下方式绑定在其他地方的公共子中:
_RigImgsDGV.DataSource = New BindingSource(_RigXMLImgsDS.Tables("Image"), Nothing)
【问题讨论】:
-
您能展示一下您是如何进行重新排序的吗?是在 DGV、视图、DT 中还是在哪里?
标签: vb.net datatable dataset xmlwriter