G
Guest
(FYI, using VB .NET 2003)
Can someone help me with this... I'm trying to read in an XML file... it
appears to work in that the DataSet ReadXML method dose not fail and then I
am able to access the table names that are in the XML file, but I'm not able
to access the rows.
Here's the code that I've got - it assumes that the fileName passed in
already exists:
Public Sub GetInput(ByVal fileName As String)
dsFileCopy = New DataSet("fileCopy")
Dim dtActions As DataTable = New DataTable("action")
Dim dtIni As DataTable = New DataTable("ini")
dtIni.Columns.Add("title", Type.GetType("System.String"))
dtIni.Columns.Add("information", Type.GetType("System.Boolean"))
dtActions.Columns.Add("source", Type.GetType("System.String"))
dtActions.Columns.Add("target", Type.GetType("System.Boolean"))
dtActions.Columns.Add("type", Type.GetType("System.Boolean"))
dsFileCopy.Tables.Add(dtIni)
dsFileCopy.Tables.Add(dtActions)
Try
dsFileCopy.ReadXml(fileName)
Catch ex As Exception
'if the file is blank or not in XML format we will fall here
Trace.WriteLine("Invalid status file " & fileName)
MsgBox(ex.ToString, MsgBoxStyle.Exclamation, "NSDUH FileCopy")
Application.Exit()
End Try
End Sub
The above code works fine, but I get a run-time error when I execute the
following statement:
Debug.WriteLine(dtIni.Rows(0).Item("information"))
The error says there is no row(0) in the table... So maybe I'm accessing the
elements of the table wrong. What I want todo is loop through each row and
perform each action based on the specifications... Am I accessing the DataSet
element correctly?
So, I tried printing all the elements in the DataSet using this code:
Private Sub PrintDataSet(ByVal dsActions As DataSet)
For Each t As DataTable In dsActions.Tables
Console.WriteLine("TableName: " + t.TableName)
For Each r As DataRow In t.Rows
For Each c As DataColumn In t.Columns
Console.Write(ControlChars.Tab + " " + r(c).ToString())
Next c
Console.WriteLine()
Next r
Next t
End Sub
But all that prints to the console is the names of the tables...
Here's the XML file:
<?xml version="1.0" standalone="yes" ?>
<fileCopy xmlns="http://tempuri.org/FileCopyInput.xsd">
<ini>
<title>Some Title here</title>
<information>Other info here</information>
</ini>
<action>
<source>.\fileA</source>
<target>C:\destinationA</target>
<type>copy</type>
</action>
<action>
<source>.\fileA</source>
<target>C:\destinationA</target>
<type>move</type>
</action>
<action>
<source>.\fileA</source>
<target>C:\destinationA</target>
<type>delete</type>
</action>
</fileCopy>
Any input would be greatly appreciated.
Charles
Can someone help me with this... I'm trying to read in an XML file... it
appears to work in that the DataSet ReadXML method dose not fail and then I
am able to access the table names that are in the XML file, but I'm not able
to access the rows.
Here's the code that I've got - it assumes that the fileName passed in
already exists:
Public Sub GetInput(ByVal fileName As String)
dsFileCopy = New DataSet("fileCopy")
Dim dtActions As DataTable = New DataTable("action")
Dim dtIni As DataTable = New DataTable("ini")
dtIni.Columns.Add("title", Type.GetType("System.String"))
dtIni.Columns.Add("information", Type.GetType("System.Boolean"))
dtActions.Columns.Add("source", Type.GetType("System.String"))
dtActions.Columns.Add("target", Type.GetType("System.Boolean"))
dtActions.Columns.Add("type", Type.GetType("System.Boolean"))
dsFileCopy.Tables.Add(dtIni)
dsFileCopy.Tables.Add(dtActions)
Try
dsFileCopy.ReadXml(fileName)
Catch ex As Exception
'if the file is blank or not in XML format we will fall here
Trace.WriteLine("Invalid status file " & fileName)
MsgBox(ex.ToString, MsgBoxStyle.Exclamation, "NSDUH FileCopy")
Application.Exit()
End Try
End Sub
The above code works fine, but I get a run-time error when I execute the
following statement:
Debug.WriteLine(dtIni.Rows(0).Item("information"))
The error says there is no row(0) in the table... So maybe I'm accessing the
elements of the table wrong. What I want todo is loop through each row and
perform each action based on the specifications... Am I accessing the DataSet
element correctly?
So, I tried printing all the elements in the DataSet using this code:
Private Sub PrintDataSet(ByVal dsActions As DataSet)
For Each t As DataTable In dsActions.Tables
Console.WriteLine("TableName: " + t.TableName)
For Each r As DataRow In t.Rows
For Each c As DataColumn In t.Columns
Console.Write(ControlChars.Tab + " " + r(c).ToString())
Next c
Console.WriteLine()
Next r
Next t
End Sub
But all that prints to the console is the names of the tables...
Here's the XML file:
<?xml version="1.0" standalone="yes" ?>
<fileCopy xmlns="http://tempuri.org/FileCopyInput.xsd">
<ini>
<title>Some Title here</title>
<information>Other info here</information>
</ini>
<action>
<source>.\fileA</source>
<target>C:\destinationA</target>
<type>copy</type>
</action>
<action>
<source>.\fileA</source>
<target>C:\destinationA</target>
<type>move</type>
</action>
<action>
<source>.\fileA</source>
<target>C:\destinationA</target>
<type>delete</type>
</action>
</fileCopy>
Any input would be greatly appreciated.
Charles