Artec Artec - 21 days ago 10
Vb.net 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\"
DataSet.ReadXml("C:\Path\sample.xml")

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)
CART_ID
in DataGridView.

My result:
enter image description here

Expected result:
enter image description here

sample.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XML_FILE>
<typ>xml</typ>
<ID>
<NR>007</NR>
</ID>
<PERSONAL>
<Surname>John</Surname>
<Name>Smith</Name>
</PERSONAL>
<COUNTRY>
<CName>UK</CName>
</COUNTRY>
<CITY>
<TOWN>
<TOWN_ID>
<PART_ID>
<CART_ID>
<SIMPLE_ID>1000</SIMPLE_ID>
<SIMPLE_AREA_ID>10.05</SIMPLE_AREA_ID>
<PLACE_ID>
<SPECIFIC_ID>
<id>A1A</id>
<name>Triangle</name>
<area>10</area>
<note>1</note>
</SPECIFIC_ID>
</PLACE_ID>
<Control>YES</Control>
<Control_area>8</Control_area>
<Control_rest>2</Control_rest>
</CART_ID>
<CART_ID>
<SIMPLE_ID>2000</SIMPLE_ID>
<SIMPLE_AREA_ID>20.05</SIMPLE_AREA_ID>
<PLACE_ID>
<SPECIFIC_ID>
<id>B1B</id>
<name>Triangle</name>
<area>20</area>
<note>2</note>
</SPECIFIC_ID>
</PLACE_ID>
<Control>YES</Control>
<Control_area>18</Control_area>
<Control_rest>2</Control_rest>
</CART_ID>
</PART_ID>
</TOWN_ID>
</TOWN>
</CITY>
</XML_FILE>


UPDATED:
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

@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.
        BuildCart("...\sample.xml")
        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

            _CartItems.Add(item)
        Next
    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.

Comments