|
|
#1 |
|
Junior Member
Join Date: Jun 07
Posts: 28
![]() |
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! |
|
|
|
|
|
#2 |
|
Hick
Join Date: Mar 05
Location: US of A
Posts: 2,325
![]() ![]() ![]() ![]() |
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> vb Code:
|
|
|
|
|
|
#3 |
|
Junior Member
Join Date: Jun 07
Posts: 28
![]() |
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. |
|
|
|
|
|
#4 |
|
Junior Member
Join Date: Jun 07
Posts: 28
![]() |
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. |
|
|
|
|
|
#5 |
|
Frenzied Member
Join Date: Oct 05
Location: Somewhere just west of the Atlantic
Posts: 1,565
![]() ![]() ![]() ![]() |
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()
Code:
v_name1 = drSession.Item("Name").ToString()
v_date = drSession.Item("SessionDate")
v_stage = drSession.Item("Stage").ToString()
__________________
Boooya
![]() Code Contributions: PHP PHP Image Gallery v1.0 • PHP Image Gallery v2.0 VB 2005 Find Computers on a network • Simple License Encryption • SQL Server Database Access dll • Use Reflection to Return Crystal ReportDocument • Silently Print PDF • Generic Xml Serailizer Useful Links: (more to come) MSDN (The first and foremost) • MSDN Design Guidelines • API Reference • Inno Setup Compiler • Inno Setup Preprocessor • ISTool - Fairly easy to use GUI for creating inno setup projects • Connection Strings • NAnt -Automated Builds • Cruise Control .NET - Frontend for automated builds
|
|
|
|
|
|
#6 |
|
Junior Member
Join Date: Jun 07
Posts: 28
![]() |
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> |
|
|
|
|
|
#7 |
|
Hick
Join Date: Mar 05
Location: US of A
Posts: 2,325
![]() ![]() ![]() ![]() |
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 |
|
|
|
|
|
#8 |
|
Junior Member
Join Date: Jun 07
Posts: 28
![]() |
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
Is the code wrong in reading the data in the xml file? |
|
|
|
|
|
#9 |
|
Hick
Join Date: Mar 05
Location: US of A
Posts: 2,325
![]() ![]() ![]() ![]() |
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. |
|
|
|
![]() |
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|