[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2006-06-14
3
Medium Priority
?
707 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
vbturbo earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 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