Artec Artec - 1 year ago 112 Question

Read xml nodes to dataset, edit values of descendants and save (Visal Studio) - reopen

This is my sample.xml file. I'm looking for instruction how to read this and put content from all nodes (subnodes) to DataSet and show it in DataGrid. I can only read single Node (without subnodes).

My code is below:

Private Sub ReadXmlButton_Click() Handles ReadXmlButton.Click

Dim filePath As String = "C:\Path\"

DataGridView1.DataSource = DataSet
DataGridView1.DataMember = "CART_ID"
End Sub

But it reads only head Node without SubNodes (1000, 10.05, YES, 8, 2). I want to display all informations (example: 1000, 10.05, A1A, Triangle, 10, 1, YES, 8, 2) from first (and all)
in DataGridView.

My result:
enter image description here

Expected result:
enter image description here


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

The last thing is how to save modified descendant (for example - manually changed SIMPPLE_ID from 1000 to 5000, etc.) to original sample.xml file ? Is it possible to do it with this solution or should I look for other way ?

Answer Source

@Artec Assuming 3 things:

  • structure of your cart node is standard
  • you know the names of the node
  • you're using Windows Form since you have the DataGridView control

... instead of using dataset, first create a class with properties that correspond to the nodes of the data you need

Public Class CartItem

    Public Property Simple_ID As String
    Public Property Simple_Area_ID As String
    Public Property Place_ID As String
    Public Property Place_Name As String

End Class

Now on your Form class, grab the data from your xml file via XDocument and create a list of CartItem.

Public Class Form1

    Private _CartItems As List(Of CartItem)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        _CartItems = New List(Of CartItem)

        'TODO: Replace ... below with your file location.
        DataGridView1.DataSource = _CartItems
    End Sub

    Private Sub BuildCart(XmlSourcePath As String)
        Dim xDoc As XDocument = XDocument.Load(XmlSourcePath)
        For Each cartElement As XElement In xDoc.Root.Descendants("CART_ID")
            Dim item As New CartItem

            With cartElement
                item.Simple_Area_ID = .Descendants("SIMPLE_ID").Value
                item.Simple_ID = .Descendants("SIMPLE_AREA_ID").Value
                item.Place_ID = .Descendants("id").Value
                item.Place_Name = .Descendants("name").Value
            End With

    End Sub
End Class

Note: there are several ways to read xml files like XMLTextReader; XMLDocument; XPathDocument; XMLReader; etc. It's just my personal preference to go with XDocument. You'll have to do some research if you want to check out the other options. It all depends on the complexity of your xml file, your requirements and preference.

This should get you going to the right direction.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download