Results 1 to 9 of 9

Thread: I can not figure out how to read multiple elements in a xml file

Hybrid View

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    35

    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. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    35

    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. #4

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    35

    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. #5
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    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

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    35

    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. #7
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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. #8

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    35

    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?

  9. #9
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width