Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XML VB.NET DataGridView
Hi,
 
I have below xml. But when I try to load into datagridview, it doesn't work.
<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..
 
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
Posted 10-Sep-12 19:30pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

do it like this
 

 
Dim ds As New DataSet
ds.ReadXml("path of excel file")
DataGridView1.Columns.Clear()
DataGridView1.DataSource = ds.Tables(0)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

try below code
string myXMLfile = @"C:\Temp\N11.xml";
    DataSet ds = new DataSet();
    // Create new FileStream with which to read the schema.
    System.IO.FileStream fsReadXml = new System.IO.FileStream 
        (myXMLfile, System.IO.FileMode.Open);
    try
    {
        ds.ReadXml(fsReadXml);
        dataGrid1.DataSource = ds.Tables[0];
        //dataGrid1.DataMember = "Cust";
        dataGrid1.DataBind();
    }
    catch (Exception ex)
    {
 
    }
    finally
    {
    fsReadXml.Close();
    }
please convert in to vb.net
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 8,478
1 OriginalGriff 6,516
2 Peter Leow 3,567
3 Zoltán Zörgő 3,351
4 Richard MacCutchan 2,397


Advertise | Privacy | Mobile
Web01 | 2.8.150123.1 | Last Updated 11 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100