Solved

Export DataGrid to XML or update data in the dataset and then export to XML

Posted on 2006-06-14
3
700 Views
Last Modified: 2011-09-20
I need to know how to either export my datagrid directly to an xml file (preferred) or modify specific items within the dataset and then export the xml.

0
Comment
Question by:JPERKS1985
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
vbturbo earned 500 total points
ID: 16910507
Hi

try this

Imports System
Imports System.Xml
Imports System.Data

Dim dsXmlData As DataSet = New DataSet
Dim strXmlFileName As String = "c:\back.xml"

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

        Me.Text = "XML to Dataset to DataGrid Example"
        Call CreateDataSource()

    End Sub

    Private Sub CreateDataSource()
        'open the xml data file
        Try
            dsXmlData.ReadXml(strXmlFileName)                               'assign the xml file to the dataset
            DataGrid1.DataSource = dsXmlData.Tables(0).DefaultView          'assign the dataset to the grids data source

            DataGrid1.DataSource.AllowNew = True                            'the add a row button still works when this is set to false
        Catch ex As System.IO.FileNotFoundException

            If CreateXmlFile() = False Then                                 'try to create the file and continue if sucesful
                MsgBox("XML file was not found")                            'something failed so we must end
                End
            End If

        End Try

    End Sub

    Private Sub AddNewRow(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRow.Click
        'when the button is clicked a rows worth of data will be added to the dataset and then written to the xml file

        Dim drnewRow As DataRow                                 'create a new datarow

        drnewRow = dsXmlData.Tables(0).NewRow()                 'assign it to our data table

        drnewRow("Type") = "File"                               'add populate it with some data
        drnewRow("Path") = "C:\hello.txt"
        drnewRow("Destination") = "D:\Backup"
        drnewRow("Id") = "5"

        dsXmlData.Tables(0).Rows.Add(drnewRow)                  'add the new data to the table/dataset

        dsXmlData.AcceptChanges()                               'and accept the addition

        'and then write it out to the xml file
        dsXmlData.WriteXml(strXmlFileName, XmlWriteMode.WriteSchema)

    End Sub

    Private Sub ProgramUnload(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed

        'if the dataset has been updated save the changes to the xml file
        If dsXmlData.HasChanges = True Then dsXmlData.WriteXml(strXmlFileName, XmlWriteMode.WriteSchema)

    End Sub

    Private Function CreateXmlFile() As Boolean
        'this will create the xml file, save it and attach it to the grid via the dataset
        'this is not the best way of doing it though. for one the ID is now a column and not an attribute of the row
        Try

            Dim dtNewRow As DataTable                               'create a table for the new row

            dtNewRow = dsXmlData.Tables.Add("Backups")              'add the table to the DataSet

            Dim c1 As New DataColumn("Type")                        'create each column in the table
            c1.DataType = System.Type.GetType("System.String")      'and set the type
            Dim c2 As New DataColumn("Path")
            c2.DataType = System.Type.GetType("System.String")      'i was having problems doing both in one line
            Dim c3 As New DataColumn("Destination")                 'it would gpf on some of them and always on the boolean
            c3.DataType = System.Type.GetType("System.String")
            Dim c4 As New DataColumn("Id")


            dtNewRow.Columns.Add(c1)                                'add the columns to the table
            dtNewRow.Columns.Add(c2)
            dtNewRow.Columns.Add(c3)
            dtNewRow.Columns.Add(c4)

            Dim drnewRow As DataRow                                 'create a new datarow

            drnewRow = dtNewRow.NewRow()                            'assign it to our data table

            drnewRow("Type") = "File"                               'add populate it with some data
            drnewRow("Path") = "C:\hello.txt"
            drnewRow("Destination") = "D:\Backup"
            drnewRow("Id") = "1"

            dtNewRow.Rows.Add(drnewRow)                             'add the new data to the table

            dsXmlData.AcceptChanges()                               'and accept the addition

            'and then write it out to the xml file
            dsXmlData.WriteXml(strXmlFileName, XmlWriteMode.WriteSchema)

            DataGrid1.DataSource = dsXmlData.Tables(0).DefaultView          'assign the dataset to the grids data source

        Catch ex As Exception
            Return False                'return a flase and just bail out
        Finally
            CreateXmlFile = True        'return true if all worked fine
        End Try

    End Function

hope this helps

rgds vbturbo
0
 
LVL 18

Expert Comment

by:vbturbo
ID: 16910529
BTW

if u wonna know about dataset's and how to work  with dataset u might checkout this link

http://www.homeandlearn.co.uk/NET/nets12p9.html
0
 
LVL 9

Expert Comment

by:lojk
ID: 16910635
I would think serialisation would be the way to go.... (because it just is, usually)

Pass your dataset into and outof these functions, they return the DS as text that you can save or whatever.
You may like to modify them to read and write directly to files..

      Public Function SerializeDataset(ByVal DatasetToSerialise As Data.Dataset) As String
        Dim oXS As System.Xml.Serialization.XmlSerializer
        Try
            oXS = New System.Xml.Serialization.XmlSerializer(GetType(Data.Dataset))
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Dim oStmW As IO.StringWriter
        oStmW = New IO.StringWriter()
      Try
            oXS.Serialize(oStmW, DatasetToSerialise)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        oStmW.Close()

        Return oStmW.ToString

    End Function



    Public Function DeserialiseDataset(ByVal SerializedObject As String) As DataSet
        Dim oXS As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(GetType(Data.DataSet))
        Dim oStmR As IO.StringReader
        oStmR = New IO.StringReader(SerializedObject)
        Dim NewDataSet As Data.DataSet= CType(oXS.Deserialize(oStmR), Data.DataSet)
        oStmR.Close()
        Return NewDataSet
    End Function



0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question