0

I want to create a program which reads xml file and puts obtained values into DataGridView. This XML file is a table dump from MySQL database. In this table I have a 'set' column where I can choose some options. Here's an example with no selected options

<size></size>

and with selected option

<size>SMALL,MEDIUM,LARGE,XLARGE</size>

Here is my code which I use to read xml file:

    Dim ofile As New OpenFileDialog
    If ofile.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim reader As XmlTextReader = New XmlTextReader(ofile.FileName)
        Using reader
            Dim toadd As String = Nothing
            Dim str() As String
            Do While (reader.Read())
                Select Case reader.NodeType
                    Case XmlNodeType.Element
                        If reader.Name.ToLower.Contains("oldIndex") Then
                            Exit Select
                        End If
                    Case XmlNodeType.Text
                        If Not reader.Value = vbNullString Or Not reader.Value = vbNullChar Then
                            toadd = toadd & reader.Value & vbTab
                        Else
                            toadd = toadd & "no data" & vbTab
                        End If
                    Case XmlNodeType.EndElement
                        If reader.Name.Contains("RECORD") Then
                            If toadd IsNot Nothing Then
                                str = toadd.Split(vbTab)
                                ShopTable.Rows.Add(str)
                                toadd = Nothing
                            End If
                        End If
                End Select
            Loop
        End Using
    End If

When there are some data between size tags, there is no problem with reading, but if there are no data, then it reads nothing and I don't receive 'no data' as a result. How do I fix this? Thanks in advance for help. Kind Regards.

1 Answer 1

0

Okay, I had to rewrite my algorithm. My xml file contained data in this format

<?xml version="1.0" standalone="yes"?>
 <RECORDS>
  <RECORD>
   <customerID>101</customerID>
   <name>Gall Anonim</name>
   <item>t-shirt</item>
   <size></size>
   ...
  </RECORD>
 </RECORDS>

I cound't get value of size field, it wasn't event empty. So I used algorithm below to get direct value of size element ;)

Dim ofile As New OpenFileDialog
If ofile.ShowDialog = Windows.Forms.DialogResult.OK Then
    Dim sr As New System.IO.StreamReader(ofile.FileName)
    Dim document As XDocument = XDocument.Parse(sr.ReadToEnd.ToString)
    Dim records = From record In document.Descendants("RECORD") _
                  Select New With _
                     { _
                        .cID = record.Element("customerID").Value, _
                        .name = Chr(&H22) & record.Element("name").Value & Chr(&H22), _
                        .item = record.Element("item").Value, _
                        .size = record.Element("size").Value _
                     }
    MsgBox(records.cID) 'shows customer id
    do some work with those variables
    ...
End If

Problem solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.