Reading XML file... getting tables but no rows...

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
 
G

Guest

I don't know the details but it looks like the contents of the xml file I was
reading in was causing the problem... maybe someone out there can provide a
through explination as to what the situation is.

Here's how my "FileCopyInput.xml" originally began:
<?xml version="1.0" standalone="yes" ?>
<fileCopy xmlns="http://tempuri.org/FileCopyInput.xsd">
:
:

When I removed (commented out) the "xmlns" attribute from the FileCopy node,
everthing worked just fine. So here's how the file looks now:

<?xml version="1.0" standalone="yes" ?>
<!--<fileCopy xmlns="http://tempuri.org/FileCopyInput.xsd">-->
<fileCopy>

Obviously I played around with defining my own xml schema and it got inseted
into the XML file I was defining. How would I change the xmlns attribute to
point to the XML schema that I defined? can I use a file:/// URL? Or specify
local host?
I'd appreciate any feedback and/or links for reading!

THanks,
Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top