Results 1 to 7 of 7

Thread: [2005] Reading from xml

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Question [2005] Reading from xml

    Hey guys,

    I was wondering if someone could help me with getting data out of my xml file and storing it in arrays

    My xml file looks as follows
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Users>
        <User Name="David Jones" Occupation="Student" OverallPercentage="50">
            	  <Activity>
    		<ActivityName>Numbers</ActivityName>
    		<ActivityLevel>Reception</ActivityLevel>
    		<RightAnswers>2</RightAnswers>
    		<WrongAnswers>2</WrongAnswers>
    		<PercentRight>50</PercentRight>
    	  </Activity>
    	  <Activity>
    		<ActivityName>Numbers</ActivityName>
    		<ActivityLevel>Year1</ActivityLevel>
    		<RightAnswers>0</RightAnswers>
    		<WrongAnswers>0</WrongAnswers>
    		<PercentRight>0</PercentRight>
    	  </Activity>
    	  <Activity>
    		<ActivityName>Numbers</ActivityName>
    		<ActivityLevel>Year2</ActivityLevel>
    		<RightAnswers>0</RightAnswers>
    		<WrongAnswers>0</WrongAnswers>
    		<PercentRight>0</PercentRight>
    	  </Activity>
    	  <Activity>
    		<ActivityName>Addition</ActivityName>
    		<ActivityLevel>Reception</ActivityLevel>
    		<RightAnswers>0</RightAnswers>
    		<WrongAnswers>0</WrongAnswers>
    		<PercentRight>0</PercentRight>
    	  </Activity>
    	  <Activity>
    		<ActivityName>Addition</ActivityName>
    		<ActivityLevel>Year1</ActivityLevel>
    		<RightAnswers>0</RightAnswers>
    		<WrongAnswers>0</WrongAnswers>
    		<PercentRight>0</PercentRight>
    	  </Activity>
    	  <Activity>
    		<ActivityName>Addition</ActivityName>
    		<ActivityLevel>Year2</ActivityLevel>
    		<RightAnswers>0</RightAnswers>
    		<WrongAnswers>0</WrongAnswers>
    		<PercentRight>0</PercentRight>
    	  </Activity>
        </User>
        <User Name = "John Smith" Occupation = "Student" OverallPercentage = "0">
            	  <Activity>
    		<ActivityName>Numbers</ActivityName>
    		<ActivityLevel>Reception</ActivityLevel>
    		<RightAnswers>0</RightAnswers>
    		<WrongAnswers>0</WrongAnswers>
    		<PercentRight>0</PercentRight>
    	  </Activity>
    	  <Activity>
    		<ActivityName>Numbers</ActivityName>
    		<ActivityLevel>Year1</ActivityLevel>
    		<RightAnswers>0</RightAnswers>
    		<WrongAnswers>0</WrongAnswers>
    		<PercentRight>0</PercentRight>
    	  </Activity>
    	  <Activity>
    		<ActivityName>Numbers</ActivityName>
    		<ActivityLevel>Year2</ActivityLevel>
    		<RightAnswers>0</RightAnswers>
    		<WrongAnswers>0</WrongAnswers>
    		<PercentRight>0</PercentRight>
    	  </Activity>
    	  <Activity>
    		<ActivityName>Addition</ActivityName>
    		<ActivityLevel>Reception</ActivityLevel>
    		<RightAnswers>0</RightAnswers>
    		<WrongAnswers>0</WrongAnswers>
    		<PercentRight>0</PercentRight>
    	  </Activity>
    	  <Activity>
    		<ActivityName>Addition</ActivityName>
    		<ActivityLevel>Year1</ActivityLevel>
    		<RightAnswers>0</RightAnswers>
    		<WrongAnswers>0</WrongAnswers>
    		<PercentRight>0</PercentRight>
    	  </Activity>
    	  <Activity>
    		<ActivityName>Addition</ActivityName>
    		<ActivityLevel>Year2</ActivityLevel>
    		<RightAnswers>0</RightAnswers>
    		<WrongAnswers>0</WrongAnswers>
    		<PercentRight>0</PercentRight>
    	  </Activity>
    	</User>
    </Users>
    and so i far i have the following code to load the xml file:

    Code:
            Dim xmlDoc As XmlDocument = New XmlDocument
            Dim xmlNodelist As XmlNodeList
            Dim xmlNode As XmlNode
            Dim i As Integer = 0
    
            xmlDoc.Load(xmlFilePath)
            xmlNodelist = xmlDoc.SelectNodes("/Users/User")
    Now what i need is for the name, occupation and overall percentage for each user to be stored in one array (which i have already set up) so that it will look as follows:

    users(0).Name = "David Jones"
    users(0).Occupation = "Student"
    users(0).OverallPercentage = 50
    users(1).Name = "John Smith"
    users(1).Occupation = "Student"
    users(1).OverallPercentage = 0

    and then i was all the data for the several activities to be stored in a seperate array (which again i already have declared) so that it will look as follows:

    activity(0).Name = "Numbers"
    activity(0).Level = "Reception"
    activity(0).Right = 2
    activity(0).wrong = 2
    activity(0).percent = 50
    activity(1).Name = "Numbers"
    activity(1).Level = "Year1"
    activity(1).Right = 0
    activity(1).Wrong = 0
    activity(1).Percent = 0
    .
    ..
    ...
    etc

    Thanks in advance for any help offered.
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Reading from xml

    Don't use an XmlDocument if you just want to read XML data. It is the least efficient way to access XML. The advantage it offers is that it offers read/write, random access to the XML data. You'd only use it when that's what you need. If all you want to do is read the data from top to bottom then the most efficient way, and therefore the correct way, is to use an XmlDataReader.

    I don't have time to post more than that for the moment but I suggest that you investigate the class and see what you can come with until I get back or someone else provides more info.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: [2005] Reading from xml

    Thanks for the reply.

    Well i will be writing to an xml file at some point in the project aswell. I am basically creating a little game, so i will need to read the data in, use it and update it within the program and the write (or update if thats possible) an xml file following the exact same layout.

    But i will investigate the xmlDataReader to see what that offers me, but if someone could post a little bit of code to help me along, it will be greatly appreciated.

    Thanks again.
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: [2005] Reading from xml

    *bump*
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

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

    Re: [2005] Reading from xml

    XSD.exe is a cool tool you can use to auto generate a schema from a XML file. With the schema you can create a strongly typed data set, which I think is what you're going for. The following assumes you've put your data in c:\myfile.xml

    1. Open the visual studio command prompt
    2. Enter xsd.exe C:\myfile.xml /outputdir:C:\
    3. Right click your project, select Add > Existing Item
    4. Select myfile.xsd, hit Add
    5. Add the following code to your app to test it
    Code:
    Dim u As New Users
    u.ReadXml("C:\myfile.xml")
    'test 
    For Each drUser As Users.UserRow In u.User
        Console.WriteLine("<<<<New User>>>>")
        Console.WriteLine(drUser.Name)
        Console.WriteLine(drUser.Occupation)
        Console.WriteLine(drUser.OverallPercentage)
        For Each drActivity As Users.ActivityRow In drUser.GetActivityRows
            Console.WriteLine(drActivity.ActivityName)
            Console.WriteLine(drActivity.ActivityLevel)
            Console.WriteLine(drActivity.RightAnswers)
            Console.WriteLine(drActivity.WrongAnswers)
            Console.WriteLine(drActivity.PercentRight)
        Next
    Next

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: [2005] Reading from xml

    Hi there,

    Thanks for the reply but is there any chance you could explain how that works as i may have to explain the process to a lecturer if he asks? Also what process would i use when i came to write to xml after i have used the information i have gotten out of the previous xml?

    Or if anybody has any code using the xmldatareader or xmldocument method as i understand how that works and will have no problem explaining to my lecturer if needed.

    Thanks again
    Last edited by Kimmy4; Apr 18th, 2008 at 04:06 AM.
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

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

    Re: [2005] Reading from xml

    Writing is easy, should be
    Code:
    u.ReadXml("C:\myfile.xml")
    All your doing is using a microsoft tool to create a strongly typed dataset based on an XML file you created. The advantages of using a strongly typed dataset, in my opinion, is intellisense through the IDE, and all type converstion is done for you. So with a strongly typed dataset you can use
    Code:
    drUser.Name
    rather than
    Code:
    drUser.Item("Name").ToString

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