在DataGridView中显示XML文件

时间:2018-01-17 16:33:56

标签: xml vb.net datagridview

我正在创建一个应用程序,用于存储公司中每个订单的XML文件。我还不太了解VB.NET,并想知道如何在DataGridView中显示文件夹内每个XML文件的行。 (每个XML文件只有一行,而模式是由VB使用XMLWriteSchema直接创建的)。 这是我用于XML编写的代码:

Dim DT As New DataTable
DT.TableName = "Ordini"
DT.Columns.Add("ID ordine")
DT.Columns.Add("Descrizione")
DT.Columns.Add("Prezzo")
DT.Columns.Add("Compratore")
DT.Columns.Add("Q.tà")
DT.Columns.Add("Vettore")
DT.Columns.Add("Data/ora")
DT.Rows.Add(txtOrderID.Text, txtDescription.Text, txtPrice.Text, txtCustomer.Text, txtQuantity.Text, cboVector.selectedValue, Datepicker.Value)
DT.WriteXml(Application.StartupPath & "\orders\order_" + txtOrderID.Text + ".xml", XmlWriteMode.WriteSchema)

如何在DataGridView中加载每个XML文件行?谢谢!

1 个答案:

答案 0 :(得分:0)

您最好的选择是序列化DataTable以使用DataTable WriteXml method创建XML,然后使用DataTable ReadXml method将其反序列化为DataTable。通过这样做,您可以将DataTable绑定到DataGridView。

您已经在使用WriteXml方法,现在只需实现ReadXml方法并绑定DataGridView:

'Create a new instance of the DataTable and read the serialized data
Dim dt As DataTable = New DataTable
dt.ReadXml("my path here.xml")

'Bind the DataGridView to the DataTable
DataGridView1.DataSource = dt