Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. DataGrid/Xml File

DataGrid/Xml File

Scheduled Pinned Locked Moved Visual Basic
questionxmlhelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Makniteasy
    wrote on last edited by
    #1

    I am working on a project called checkbook.I have an xml file called checkbook.xml in a file called checkbook. I also have a datagrid on my form called DataGrid1. My xmlfile looks like this: 01533Kmart55.2311/12/2004

    When I run my program my datagrid already shows what is in the xml file because I am using streamreader. How can I get The information entered in the datagrid by the user to save back to my xml file upon program close so it will be there the next time the program is opened. This is the code that I have so far: Imports System.Xml Imports System.IO Imports System.Data Public Class Form1 Inherits System.Windows.Forms.Form Public myxmlfile As String = "c:\checkbook\checkbook.xml" Public ods As DataSet Public nds As DataSet Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try Dim ods As New DataSet Dim myStreamReader As New System.IO.StreamReader(myxmlfile) ods.ReadXml(myStreamReader) myStreamReader.Close() Me.DataGrid1.DataSource = ods.Tables(0) Me.DataGrid1.FlatMode = True Me.DataGrid1.Expand(-1) Me.DataGrid1.ColumnHeadersVisible = True Catch ex As Exception MsgBox(ex.ToString) End Try End Sub End Class Thanks For The Help

    G 1 Reply Last reply
    0
    • M Makniteasy

      I am working on a project called checkbook.I have an xml file called checkbook.xml in a file called checkbook. I also have a datagrid on my form called DataGrid1. My xmlfile looks like this: 01533Kmart55.2311/12/2004

      When I run my program my datagrid already shows what is in the xml file because I am using streamreader. How can I get The information entered in the datagrid by the user to save back to my xml file upon program close so it will be there the next time the program is opened. This is the code that I have so far: Imports System.Xml Imports System.IO Imports System.Data Public Class Form1 Inherits System.Windows.Forms.Form Public myxmlfile As String = "c:\checkbook\checkbook.xml" Public ods As DataSet Public nds As DataSet Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try Dim ods As New DataSet Dim myStreamReader As New System.IO.StreamReader(myxmlfile) ods.ReadXml(myStreamReader) myStreamReader.Close() Me.DataGrid1.DataSource = ods.Tables(0) Me.DataGrid1.FlatMode = True Me.DataGrid1.Expand(-1) Me.DataGrid1.ColumnHeadersVisible = True Catch ex As Exception MsgBox(ex.ToString) End Try End Sub End Class Thanks For The Help

      G Offline
      G Offline
      Gavin Jeffrey
      wrote on last edited by
      #2

      Couldn't you just use the writeXml method of the dataset?

      M 2 Replies Last reply
      0
      • G Gavin Jeffrey

        Couldn't you just use the writeXml method of the dataset?

        M Offline
        M Offline
        Makniteasy
        wrote on last edited by
        #3

        Yeah but everytime I use streamwriter it says that my xmlfile is missing a root element. what is that?

        D 1 Reply Last reply
        0
        • G Gavin Jeffrey

          Couldn't you just use the writeXml method of the dataset?

          M Offline
          M Offline
          Makniteasy
          wrote on last edited by
          #4

          Yeah but everytime I use streamwriter it says that my xmlfile is missing a root element. what is that?

          G 2 Replies Last reply
          0
          • M Makniteasy

            Yeah but everytime I use streamwriter it says that my xmlfile is missing a root element. what is that?

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            The error shows up because you have a completely malformed XML file:

            <table>
            0
            1533
            Kmart
            55.23
            11/12/2004
            </table>

            Your XML should look more like this:

            <?xml version="1.0" encoding="utf-8" ?>
            <Accounts>
            <CheckingAccount>
            <Transactions>
            <Transaction ID="0">
            <CheckNumber>1533</CheckNumber>
            <Payee>Kmart</Payee>
            <Amount>55.23</Amount>
            <TransDate>11/12/2004</TransDate>
            </Transaction>
            </Transactions>
            </CheckingAccount>
            </Accounts>

            RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            1 Reply Last reply
            0
            • M Makniteasy

              Yeah but everytime I use streamwriter it says that my xmlfile is missing a root element. what is that?

              G Offline
              G Offline
              Gavin Jeffrey
              wrote on last edited by
              #6

              What Dave says is true your xml structure is very poor (no offense - it just is). Every valid xml document should have a root element that contains all the other elements - at the moment you only have the root element which isn't allowed to contain anything but other elements. So if you did something like ...

              it would work (the saving part anyways). check out this tutorial (it isnt too long and it explanes pretty much everything you need to know about xml) - http://www.w3schools.com/xml/default.asp[^]

              1 Reply Last reply
              0
              • M Makniteasy

                Yeah but everytime I use streamwriter it says that my xmlfile is missing a root element. what is that?

                G Offline
                G Offline
                Gavin Jeffrey
                wrote on last edited by
                #7

                hmm thats strange for some reason my post didn't come out as I wrote it - the xml example that I give should be like this ...

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups