To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
MSDN Subscribers: Download the VS 2010 Release Candidate
MSDN Subscribers: Download the VS 2010 Release Candidate
Sell Your Code and Make Money?
Creating your own Tetris game using VB.NET
Article :: Improving Software Economics, Part 4 of 7: Top 10 Principles of Iterative Software Management



Go Back   VBForums > Visual Basic > Visual Basic .NET

Reply Post New Thread
 
Thread Tools Search this Thread Display Modes
Old Jun 4th, 2007, 11:33 AM   #1
kevin_10987
Junior Member
 
Join Date: Jun 07
Posts: 20
kevin_10987 is on a distinguished road (10+)
I can not figure out how to read multiple elements in a xml file

My xml file is named "sessiondata.xml". The syntax is correct. My XMLWrite() sub procedure works and writes the root, and elements correctly. So that part works. But I am not for sure on how to retrieve elements that have multiple elements. Okay, I'll just show you the content of my xml file and then my sub procedure that I written to read the xml file.

<?xml version="1.0" ?>
- <SessionData>
- <Session>
<Name>Kevin Howell</Name>
<SessionDate>6/15/2007</SessionDate>
<Stage>Stage 5</Stage>
<AmountOfData>6</AmountOfData>
<Exercise>bench pressing</Exercise>
<Exercise>bench pressing</Exercise>
<Exercise>bench pressing</Exercise>
<Exercise>bench pressing</Exercise>
<Exercise>bench pressing</Exercise>
<Exercise>bench pressing</Exercise>
<NumberOfReps>2</NumberOfReps>
<NumberOfReps>2</NumberOfReps>
<NumberOfReps>2</NumberOfReps>
<NumberOfReps>2</NumberOfReps>
<NumberOfReps>2</NumberOfReps>
<NumberOfReps>2</NumberOfReps>
<NumberOfSets>1</NumberOfSets>
<NumberOfSets>1</NumberOfSets>
<NumberOfSets>1</NumberOfSets>
<NumberOfSets>1</NumberOfSets>
<NumberOfSets>1</NumberOfSets>
<NumberOfSets>1</NumberOfSets>
<Rest>1</Rest>
<Rest>1</Rest>
<Rest>1</Rest>
<Rest>1</Rest>
<Rest>1</Rest>
<Rest>1</Rest>
<Dimension>min</Dimension>
<Dimension>min</Dimension>
<Dimension>min</Dimension>
<Dimension>sec</Dimension>
<Dimension>sec</Dimension>
<Dimension>sec</Dimension>
</Session>
</SessionData>

Here is my code (using Visual Basic 2005 Express Edition):

Public Sub XMLRead()
'clear the arraylist
AddSessionDataForm.sessionDataArrayList.Clear()
Dim m_xmlr As XmlTextReader
'Create the XML Reader
m_xmlr = New XmlTextReader("sessiondata.xml")
'Disable whitespace so that you don't have to read over whitespaces
m_xmlr.WhitespaceHandling = WhitespaceHandling.None

'Load the Loop
While Not m_xmlr.EOF
'if not start element exit while loop
If Not m_xmlr.IsStartElement() Then
Exit While
End If

Try
TryAgain:
Dim i As Integer = 0
Dim anExerciseArrayList As New ArrayList
Dim aNumberOfRepsArrayList As New ArrayList
Dim aNumberofSetsArrayList As New ArrayList
Dim aRest As New ArrayList
Dim aDimensionArrayList As New ArrayList
Dim amountOfData As Integer = 0

AddSessionDataForm.sessiondata.Name() = m_xmlr.ReadElementString("Name")
AddSessionDataForm.sessiondata.SessionDate() = m_xmlr.ReadElementString("SessionDate")
AddSessionDataForm.sessiondata.Stage() = m_xmlr.ReadElementString("Stage")
amountOfData = m_xmlr.ReadElementString("AmountOfData")

For i = 0 To amountOfData - 1
AddSessionDataForm.sessiondata.Exercise() = m_xmlr.ReadElementString("Exercise")
anExerciseArrayList.Add(AddSessionDataForm.sessiondata.Exercise())
Next

For i = 0 To amountOfData - 1
AddSessionDataForm.sessiondata.NumberOfReps() = m_xmlr.ReadElementString("NumberOfReps")
aNumberOfRepsArrayList.Add(AddSessionDataForm.sessiondata.NumberOfReps())
Next


For i = 0 To amountOfData - 1
AddSessionDataForm.sessiondata.NumberOfSets() = m_xmlr.ReadElementString("NumberOfSets")
aNumberofSetsArrayList.Add(AddSessionDataForm.sessiondata.NumberOfSets())
Next

For i = 0 To amountOfData - 1
AddSessionDataForm.sessiondata.Rest() = m_xmlr.ReadElementString("Rest")
aRest.Add(AddSessionDataForm.sessiondata.Rest())
Next

For i = 0 To amountOfData - 1
AddSessionDataForm.sessiondata.Dimension() = m_xmlr.ReadElementString("Dimension")
aDimensionArrayList.Add(AddSessionDataForm.sessiondata.Dimension())
Next

Dim sessionData As New SessionData(AddSessionDataForm.sessiondata.Name(), _
AddSessionDataForm.sessiondata.SessionDate(), AddSessionDataForm.sessiondata.Stage(), _
AddSessionDataForm.sessiondata.Exercise(), AddSessionDataForm.sessiondata.NumberOfReps(), _
AddSessionDataForm.sessiondata.NumberOfSets(), AddSessionDataForm.sessiondata.Rest(), _
AddSessionDataForm.sessiondata.Dimension())
AddSessionDataForm.sessionDataArrayList.Add(sessionData)
Catch ex As Exception
m_xmlr.Read()
GoTo TryAgain
End Try
m_xmlr.Read() 'Moves pass the attribute and onto the elements
End While
'close the reader
m_xmlr.Close()
End Sub

If anyone can help me figure out how I can read each one of the exercise elements, I could apply it to the other elements. There are some problems with the way I written my SessionData problem domain class that I am going to have to re-write it so that the Exercise procedure will return an array of information instead of just one. So I realize that problem now, but I really like to know how to read each element when an xml file contains multiple elements.

Thanks!
kevin_10987 is offline   Reply With Quote
Old Jun 4th, 2007, 04:10 PM   #2
wild_bill
Hick
 
wild_bill's Avatar
 
Join Date: Mar 05
Location: US of A
Posts: 2,280
wild_bill is a jewel in the rough (300+)wild_bill is a jewel in the rough (300+)wild_bill is a jewel in the rough (300+)wild_bill is a jewel in the rough (300+)
Re: I can not figure out how to read multiple elements in a xml file

If I were you I would set the XML up like this
Code:
<?xml version="1.0" ?> 
<SessionData>
	<Session>
		<Name>Kevin Howell</Name> 
		<SessionDate>6/15/2007</SessionDate> 
		<Stage>Stage 5</Stage> 
		<AmountOfData>
			<Exercise>bench pressing</Exercise>
			<NumberOfReps>2</NumberOfReps> 
			<NumberOfSets>1</NumberOfSets> 
			<Rest>1</Rest> 
			<Dimension>min</Dimension> 
		</AmountOfData> 
		<AmountOfData>
			<Exercise>bench pressing</Exercise>
			<NumberOfReps>2</NumberOfReps> 
			<NumberOfSets>1</NumberOfSets> 
			<Rest>1</Rest> 
			<Dimension>min</Dimension> 
		</AmountOfData> 
		<AmountOfData>
			<Exercise>bench pressing</Exercise>
			<NumberOfReps>2</NumberOfReps> 
			<NumberOfSets>1</NumberOfSets> 
			<Rest>1</Rest> 
			<Dimension>min</Dimension> 
		</AmountOfData> 
		<AmountOfData>
			<Exercise>bench pressing</Exercise>
			<NumberOfReps>2</NumberOfReps> 
			<NumberOfSets>1</NumberOfSets> 
			<Rest>1</Rest> 
			<Dimension>sec</Dimension> 
		</AmountOfData> 
		<AmountOfData>
			<Exercise>bench pressing</Exercise>
			<NumberOfReps>2</NumberOfReps> 
			<NumberOfSets>1</NumberOfSets> 
			<Rest>1</Rest> 
			<Dimension>sec</Dimension> 
		</AmountOfData> 
		<AmountOfData>
			<Exercise>bench pressing</Exercise>
			<NumberOfReps>2</NumberOfReps> 
			<NumberOfSets>1</NumberOfSets> 
			<Rest>1</Rest> 
			<Dimension>sec</Dimension> 
		</AmountOfData> 		
	</Session>
</SessionData>
and parse it like this
vb Code:
  1. Dim ds As New DataSet
  2.         ds.ReadXml("..\Config\Test.xml")
  3.         For Each drSession As DataRow In ds.Tables("Session").Rows
  4.             Console.WriteLine(drSession("Name").ToString)
  5.             Console.WriteLine(drSession("SessionDate").ToString)
  6.             Console.WriteLine(drSession("Stage").ToString)
  7.             For Each drAmountOfData As DataRow In drSession.GetChildRows("Session_AmountOfData")
  8.                 Console.WriteLine(drAmountOfData("Exercise").ToString)
  9.                 Console.WriteLine(drAmountOfData("NumberOfReps").ToString)
  10.                 Console.WriteLine(drAmountOfData("NumberOfSets").ToString)
  11.                 Console.WriteLine(drAmountOfData("Rest").ToString)
  12.                 Console.WriteLine(drAmountOfData("Dimension").ToString)
  13.             Next
  14.         Next
wild_bill is offline   Reply With Quote
Old Jun 5th, 2007, 05:45 AM   #3
kevin_10987
Junior Member
 
Join Date: Jun 07
Posts: 20
kevin_10987 is on a distinguished road (10+)
Re: I can not figure out how to read multiple elements in a xml file

Thanks for the solution! I'll try it out and post back here with the results.

I like your solution for my xml file better. I am going to use it. It is a lot better formatted.

Thanks, I will report back with my results.
kevin_10987 is offline   Reply With Quote
Old Jun 7th, 2007, 07:50 AM   #4
kevin_10987
Junior Member
 
Join Date: Jun 07
Posts: 20
kevin_10987 is on a distinguished road (10+)
Re: I can not figure out how to read multiple elements in a xml file

I am having some difficulties getting this code to work.

This is an image of the program encountering the error.



Here is the code that reads the xml file.

Code:
Public Sub XMLRead()
        Dim ds As New DataSet
        Dim v_name1 As String
        Dim v_date As Date
        Dim v_stage As String = ""

        Dim exerciseArray As New ArrayList
        Dim numberOfSetsArray As New ArrayList
        Dim numberOfRepsArray As New ArrayList
        Dim restArray As New ArrayList
        Dim dimensionArray As New ArrayList
        'clear arraylist
        exerciseArray.Clear()
        numberOfSetsArray.Clear()
        numberOfRepsArray.Clear()
        restArray.Clear()
        dimensionArray.Clear()
        ds.ReadXml("sessiondata.xml")
        For Each drSession As DataRow In ds.Tables("Session").Rows
            v_name1 = ds.Tables("Name").ToString()
            v_date = ds.Tables("SessionDate").ToString()
            v_stage = ds.Tables("Stage").ToString()
            For Each drData As DataRow In drSession.GetChildRows("Session_Data")
                exerciseArray.Add(drData("Exercise").ToString())
                numberOfSetsArray.Add(drData("NumberOfSets").ToString())
                numberOfRepsArray.Add(drData("NumberOfReps").ToString())
                restArray.Add(drData("Rest").ToString())
                dimensionArray.Add(drData("Dimension").ToString())
            Next
            AddSessionDataForm.sessionDataArrayList.Add(New SessionData(v_name1, v_date, v_stage, _
                         exerciseArray, numberOfSetsArray, numberOfRepsArray, restArray, _
                         dimensionArray))
        Next
    End Sub

If someone can figure out what I can do to make this sub procedure run, please inform me.
kevin_10987 is offline   Reply With Quote
Old Jun 7th, 2007, 09:46 AM   #5
bmahler
Frenzied Member
 
bmahler's Avatar
 
Join Date: Oct 05
Location: Somewhere just west of the Atlantic
Posts: 1,564
bmahler is a jewel in the rough (300+)bmahler is a jewel in the rough (300+)bmahler is a jewel in the rough (300+)bmahler is a jewel in the rough (300+)
Re: I can not figure out how to read multiple elements in a xml file

these

Code:
v_name1 = ds.Tables("Name").ToString()
v_date = ds.Tables("SessionDate").ToString()
v_stage = ds.Tables("Stage").ToString()
Should be

Code:
v_name1 = drSession.Item("Name").ToString()
v_date = drSession.Item("SessionDate")
v_stage = drSession.Item("Stage").ToString()
In your code you are trying to access tables that don't exist. You need to get the data from the DataRow which you defined as drSession. Also you should not be using ToString on a field that is a date variable. If it is a date, there is no reason to convert it to a string.
__________________
Boooya
  • Visual Studio 2008 Professional
  • Don't forget to use [CODE]your code here[/CODE] when posting code
  • Don't forget to rate helpful posts!
  • If you're question was answered please mark your thread [Resolved]

Code Contributions:
PHP
PHP Image Gallery v1.0PHP Image Gallery v2.0
VB 2005
Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


Useful Links: (more to come)
MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds
bmahler is offline   Reply With Quote
Old Jun 7th, 2007, 02:48 PM   #6
kevin_10987
Junior Member
 
Join Date: Jun 07
Posts: 20
kevin_10987 is on a distinguished road (10+)
Re: I can not figure out how to read multiple elements in a xml file

Thanks bmahler, you were correct. That fixed that problem. Now I have one more problem and then my XMLRead() sub procedure should work.

Here is a image of the problem.



I have already posted my code, so I won't take up anymore space by posting it again.

Here is what my xml file looks like now.

Code:
<?xml version="1.0" ?> 
<SessionData>
	<Session>
		<Name>Kevin Howell</Name> 
		<SessionDate>6/15/2007</SessionDate> 
		<Stage>Stage 5</Stage> 
		<Data>
			<Exercise>bench pressing</Exercise>
			<NumberOfReps>2</NumberOfReps> 
			<NumberOfSets>1</NumberOfSets> 
			<Rest>1</Rest> 
			<Dimension>min</Dimension> 
		</Data> 
		<Data>
			<Exercise>bench pressing</Exercise>
			<NumberOfReps>2</NumberOfReps> 
			<NumberOfSets>1</NumberOfSets> 
			<Rest>1</Rest> 
			<Dimension>min</Dimension> 
		</Data> 
		<Data>
			<Exercise>bench pressing</Exercise>
			<NumberOfReps>2</NumberOfReps> 
			<NumberOfSets>1</NumberOfSets> 
			<Rest>1</Rest> 
			<Dimension>min</Dimension> 
		</Data> 
		<Data>
			<Exercise>bench pressing</Exercise>
			<NumberOfReps>2</NumberOfReps> 
			<NumberOfSets>1</NumberOfSets> 
			<Rest>1</Rest> 
			<Dimension>sec</Dimension> 
		</Data> 
		<Data>
			<Exercise>bench pressing</Exercise>
			<NumberOfReps>2</NumberOfReps> 
			<NumberOfSets>1</NumberOfSets> 
			<Rest>1</Rest> 
			<Dimension>sec</Dimension> 
		</Data> 
		<Data>
			<Exercise>bench pressing</Exercise>
			<NumberOfReps>2</NumberOfReps> 
			<NumberOfSets>1</NumberOfSets> 
			<Rest>1</Rest> 
			<Dimension>sec</Dimension> 
		</Data> 		
	</Session>
</SessionData>
kevin_10987 is offline   Reply With Quote
Old Jun 7th, 2007, 05:19 PM   #7
wild_bill
Hick
 
wild_bill's Avatar
 
Join Date: Mar 05
Location: US of A
Posts: 2,280
wild_bill is a jewel in the rough (300+)wild_bill is a jewel in the rough (300+)wild_bill is a jewel in the rough (300+)wild_bill is a jewel in the rough (300+)
Re: I can not figure out how to read multiple elements in a xml file

Set a break on that line and run your program, when the program breaks, right click ds and click QuickWatch. In the input textbox enter
ds.tables("Data").Columns(0).ColumnName and hit recalculate
What do you get? Then do the following, and see what you get
ds.tables("Data").Columns(1).ColumnName and hit recalculate
ds.tables("Data").Columns(2).ColumnName and hit recalculate
ds.tables("Data").Columns(3).ColumnName and hit recalculate
ds.tables("Data").Columns(4).ColumnName and hit recalculate
wild_bill is offline   Reply With Quote
Old Jun 7th, 2007, 07:07 PM   #8
kevin_10987
Junior Member
 
Join Date: Jun 07
Posts: 20
kevin_10987 is on a distinguished road (10+)
Re: I can not figure out how to read multiple elements in a xml file

Okay, I did what you said, and here is what I got.

Code:
ds.tables("Data").Columns(0).ColumnName 
value = "Data_ID"

ds.tables("Data").Columns(1).ColumnName 
value = "Session_ID"

ds.tables("Data").Columns(2).ColumnName 
Index out of range exception
The index out of range exception occurs after the 2nd index item also.

Is the code wrong in reading the data in the xml file?
kevin_10987 is offline   Reply With Quote
Old Jun 8th, 2007, 10:26 AM   #9
wild_bill
Hick
 
wild_bill's Avatar
 
Join Date: Mar 05
Location: US of A
Posts: 2,280
wild_bill is a jewel in the rough (300+)wild_bill is a jewel in the rough (300+)wild_bill is a jewel in the rough (300+)wild_bill is a jewel in the rough (300+)
Re: I can not figure out how to read multiple elements in a xml file

I loaded your xml file and ran this line
ds.Tables("Session").Rows(0).GetChildRows("Session_Data")(0).Item(0)
The result is "bench pressing", so I don't know what's going on.
wild_bill is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic .NET


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 12:49 PM.




To view more projects, click here

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.