help populating a dataGrid with a table from XML

Posted on 2005-04-13
Last Modified: 2010-04-17
Hi...i just want to load a dataGrid with a table in my XML doc.  i just need to know if i need to change my XML, my VB.NET code or both and how......thanks!  all the details are below; if i 've forgotten anything, just ask please!

XML CODE snippet:
      <character id="0" type="PC" name="Nathan Baros" realName="Craig Pierce">
                <skill name="climb" level="6" />
                <skill name="sit" level="10" />
                <skill name="stand" level="2" />
      <character id="0" type="PC" name="Todd Myres" realName="Josh">
                  <skill name="falldown" level="6" />
                  <skill name="guitar" level="6" />

VB.NET CODE snippet:
charactersDS.ReadXml("C:\Characters.xml", XmlReadMode.InferSchema)
charactersDT = charactersDS.Tables("character")

when i say *charactersDT = charactersDS.Tables("character")* i can then refer to each 'character' as a in  *RealNameTextBox.Text = charactersDT.Rows(RowNumber).Item("realName")*

each character's stats will go into text boxes using lines just like that one.  then i have a dataGrid that i'm trying to place the skills into.  i can make the dataGrid display ALL of the skills for EVERY character with "Name" and "Level" columns at the top and the skills listed in the rows.....using this code:

Dim skillsDT As DataTable
skillsDT = charactersDS.Tables("skill")
DataGridView1.DataSource = skillsDT

that grabs every 'skill' for every 'character' in the XML document; however, i only want to display the skills of ONE character at a time.

is there a way that i can specify one dataTable to consist of only the 3 skills for Nathan Baros and another dataTable to consist of only the 2 skills for Todd Myres??  can i somehow specify which row(i.e. character) i want to take the skills from?  (i.e. only the skills from charactersDT.Rows(RowNumber))

Note:  i am using a dataListView and listing all the characters in that listView....then, when you select one character, i want that one character's skills to appear in the dataGrid.

please Help!  this is driving me nuts......thanks.
Question by:craigpierce
    LVL 25

    Accepted Solution

    Hi graigpierce, This work I have changed the xml file a bit (I didn't see the use of the skills)


          <character id="0" type="PC" name="Nathan Baros" realName="Craig Pierce">
               <skill name="climb" level="6" />
               <skill name="sit" level="10" />
               <skill name="stand" level="2" />
          <character id="1" type="PC" name="Todd Myres" realName="Josh">
               <skill name="falldown" level="6" />
               <skill name="guitar" level="6" />

    if you want just to show the skills of Todd Myres, do something like this

            Dim DS As New DataSet
            DS.ReadXml("C:\ttt.xml", XmlReadMode.InferSchema)
            DataGrid1.DataSource = DS.Tables(0)
            Dim dv As DataView = DS.Tables(1).DefaultView
            dv.Sort = "Character_id"
            dv.RowFilter = "Character_id = 1"
            DataGrid2.DataSource = dv
    LVL 1

    Author Comment

    thank you!  that was easy (as i knew it had to be)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    improve Shell script 5 115
    thread-safe code in c++ 2 58
    mapBully challenge 6 50
    Program to display an alert on Windows Toolbar 2 25
    I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now