Click here to Skip to main content
15,895,557 members

Load xml into datagridview

jameslooi88 asked:

Open original thread
Hi,

I have below xml. But when I try to load into datagridview, it doesn't work.
XML
<Records>
 <Record>
 <Product>
  <Column name="Serial">N110879987</Column>
  <Column name="Product">QWX4T6YU</Column>
  <Column name="Date">2012-08-10</Column>
 </Product>
 <Operations>
 <Operation type="SETUP">
  <Column name="TimeStamp">2012-08-10</Column>
  <Column name="Status">Success</Column>
 </Operation>
 <Operation type="RUN">
  <Column name="TimeStamp">2012-08-11</Column>
  <Column name="Status">Done</Column>
 </Operation>
 </Operations>
</Record>
 <Record>
 <Product>
  <Column name="Serial">N110879988</Column>
  <Column name="Product">QWX4T6YU</Column>
  <Column name="Date">2012-08-12</Column>
 </Product>
 <Operations>
 <Operation type="SETUP">
  <Column name="TimeStamp">2012-08-12</Column>
  <Column name="Status">Success</Column>
 </Operation>
 <Operation type="RUN">
  <Column name="TimeStamp">2012-08-12</Column>
  <Column name="Status">Done</Column>
 </Operation>
 </Operations>
</Record>
</Records>


Below is my code, it doesn't work..

VB
Dim dt As New DataTable
Dim newRow As DataRow
Dim ds As New DataSet()
Dim doc As New Xml.XmlDocument
doc.Load("C:\Temp\N11.xml")

        Dim dt As New DataTable
        Dim newRow As DataRow
        dt.Columns.Add("Serial")
        dt.Columns.Add("Product")
        dt.Columns.Add("Date")

        dt.Columns.Add("Operation")
        dt.Columns.Add("TimeStamp")
        dt.Columns.Add("Status")


For Each n As XmlNode In doc.SelectNodes("Records/Record/Product/Column")
            newRow = dt.NewRow

            newRow(n.Attributes("name").Value) = n.InnerText



            For Each n1 As XmlNode In doc.SelectNodes("Records/Record/Operations/Operation")
                newRow = dt.NewRow
                newRow("Operation") = n1.Attributes("type").Value

                For Each n2 As XmlNode In doc.SelectNodes("Records/Record/Operations/Operation[@type='" & n1.Attributes("type").Value & "']/Column")

                    newRow(n2.Attributes("name").Value) = n2.InnerText

                Next
                dt.Rows.Add(newRow)
            Next
        Next

  DataGridView1.DataSource = dt


Below is my idle output, can anyone help?
Serial      Product   Date      Operation  TimeStamp  Status
N110879987  QWX4T6YU  8/10/2012	SETUP      8/10/2012  Success<code></code>
			        RUN	   8/11/2012  Done
N110879988  QWX4T6YU  8/12/2012	SETUP      8/12/2012  Success
			        RUN	   8/12/2012  Done
Tags: Visual Basic, XML, DataGridView

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900