Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all
I want to import a Xml file to datagridview . please help me.
Posted

Read the Xml file using DataSet or DataTable's
ReadXml() method
Bind the DataGridView to the DataSet/DataTable
 
Share this answer
 
hi i have a compelete solution project for importing and exporting xml files to datagridview and vise versa.
i hope my project will be useful for you.
follow these steps:
1- create new windows forms application project
2- drag a datagridview and rename it to: dgv
3- drag 3 buttons: and change their names to btnRead, btnWrite, btnClear
and their text as "Read XML","Write XML","Clear"
4- double click on the form1 and press control+A to select all text and replace it by the codes below:

VB
Public Class Form1

    Dim dt As DataTable
    Dim fs As IO.Stream
    Dim ss As IO.Stream

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        Dim sd As New OpenFileDialog
        sd.Filter = "XML ducuments(*.XML)|*.xml"
        sd.DefaultExt = "xml"
        If sd.ShowDialog = DialogResult.OK Then
            fs = sd.OpenFile
            Change_Datatable()
            If IO.File.Exists(IO.Path.ChangeExtension(sd.FileName, ".sch")) Then
                ss = New IO.StreamReader(IO.Path.ChangeExtension(sd.FileName, ".sch")).BaseStream
                dt.ReadXmlSchema(ss)
                ss.Close()
            Else
                MsgBox("فایل حاوی تنظیمات دیتابیسی(XML-Schema) برای XML انتخاب شده یافت نشد.", MsgBoxStyle.MsgBoxRtlReading Or MsgBoxStyle.MsgBoxRight)
                Return
            End If
            dt.ReadXml(fs)
            fs.Close()
        End If
    End Sub

    Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
        Dim sd As New SaveFileDialog
        sd.Filter = "XML ducuments(*.XML)|*.xml"
        sd.DefaultExt = "xml"
        If sd.ShowDialog = DialogResult.OK Then
            fs = sd.OpenFile
            ss = New IO.StreamWriter(IO.Path.ChangeExtension(sd.FileName, ".sch")).BaseStream
            dt.WriteXmlSchema(ss)
            dt.WriteXml(fs)
            fs.Close()
            ss.Close()
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        dt.Clear()
    End Sub

    Public Sub Change_Datatable()
        dt = New DataTable
        dt.TableName = "Tables(0)"
        dgv.DataSource = dt
    End Sub

End Class
 
Share this answer
 

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



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