DataGrid/Xml File
-
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
-
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
Couldn't you just use the writeXml method of the dataset?
-
Couldn't you just use the writeXml method of the dataset?
Yeah but everytime I use streamwriter it says that my xmlfile is missing a root element. what is that?
-
Couldn't you just use the writeXml method of the dataset?
Yeah but everytime I use streamwriter it says that my xmlfile is missing a root element. what is that?
-
Yeah but everytime I use streamwriter it says that my xmlfile is missing a root element. what is that?
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
-
Yeah but everytime I use streamwriter it says that my xmlfile is missing a root element. what is that?
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[^]
-
Yeah but everytime I use streamwriter it says that my xmlfile is missing a root element. what is that?
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 ...