Link to home
Start Free TrialLog in
Avatar of sapbucket
sapbucket

asked on

Loading XML Data on a DataGrid Control?

Hi, I'm trying to load the xml on the bottom of this post into a datagrid control. I'm using Visual Studio 2003  (VB.NET Windows Application). I built a form with two buttons and a datagrid and a data set. The button click events should be populating the datagrid but they do not. Any help experts?

The error I get:
An unhandled exception of type 'System.Xml.XmlException' occurred in system.xml.dll

Additional information: System error.
// And it highlights the following, indicating where the code break occurs:
DataSet1.ReadXml(RStream)



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim RStream As New System.IO.StreamReader("c:\rte\dev\" & "PublishersTitles.xml")
        DataSet1.Clear()
        DataSet1.ReadXml(RStream)
        DataSet1.Relations.Clear()
        DataSet1.Relations.Add(New DataRelation("Pubs2Titles", DataSet1.Tables(0).Columns("pub_id"), DataSet1.Tables(1).Columns("pub_id")))
        DataGrid1.DataSource = DataSet1
        RStream.Close()

        'Dataset.ReadXml("c:\testXMLOUT.xml")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim WStream As New System.IO.StreamWriter("c:\rte\dev\" & "PublishersTitles.xml")
        DataSet1.WriteXml(WStream, XmlWriteMode.DiffGram)
        WStream.Close()
        DataSet1.Clear()
    End Sub
End Class




XML - PublishersTitles.xml

<publishers>
 <pub_id>0736</pub_id>
 <pub_name>New Moon Books</pub_name>
 <titles>
  <title_id>BU2075</title_id>
  <title>You Can Combat Computer Stress!</title>
  <pub_id>0736</pub_id>
  <pubdate>1991-06-30T00:00:00.0000000+03:00</pubdate>
 </titles>
 <titles>
  <title_id>PS2091</title_id>
  <title>Is Anger The Enemy?</title>
  <pub_id>0736</pub_id>
  <price>10.95</price>
  <pubdate>1991-06-15T00:00:00.0000000+03:00</pubdate>
 </titles>
</publishers>
<publishers>
 <pub_id>1389</pub_id>
 <pub_name>Algodata Infosystems</pub_name>
 <titles>
  <title_id>BU1032</title_id>
  <title>The Busy Executive's Database Guide</title>
  <pub_id>1389</pub_id>
  <price>19.99</price>
  <pubdate>1991-06-12T00:00:00.0000000+03:00</pubdate>
 </titles>
</publisher>
Avatar of sapbucket
sapbucket

ASKER

Well, I go this walkthrough working:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbwlkWalkthroughAccessingXMLData.asp

but when I switched to a different xml file (not authors.xml) to what I have above (PublishersTitle.xml) it did not work.

Is there a certain format of xml I should use? Could it be that the xml format I have above doesn't work?
Well you xml file is definitly mall formed (you have multiple root elements for instance)

I can not make out from the file what belongs to what, so how would the xml parser :-)

Is this what you want ?

Root element Publishers
                        PubID
                        Pub_Name
                            Titles
                                 title_id
                                 title
                                 pubdate
                                 Price


then it should look like this

<publishers>
      <publisher>
            <pub_id>0736</pub_id>
            <pub_name>New Moon Books</pub_name>
            <titles>
                  <title_id>BU2075</title_id>
                  <title>You Can Combat Computer Stress!</title>
                  <price></price>
                  <pubdate>1991-06-30T00:00:00.0000000+03:00</pubdate>
            </titles>
            <titles>
                  <title_id>PS2091</title_id>
                  <title>Is Anger The Enemy?</title>
                  <price>10.95</price>
                  <pubdate>1991-06-15T00:00:00.0000000+03:00</pubdate>
            </titles>
      </publisher>
      <publisher>
            <pub_id>1389</pub_id>
            <pub_name>Algodata Infosystems</pub_name>
            <titles>
                  <title_id>BU1032</title_id>
                  <title>The Busy Executive's Database Guide</title>
                  <price>19.99</price>
                  <pubdate>1991-06-12T00:00:00.0000000+03:00</pubdate>
            </titles>
      </publisher>
</publishers>
ASKER CERTIFIED SOLUTION
Avatar of RonaldBiemans
RonaldBiemans

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cool - I was able to populate the DataGrid using the XML you provided. So really my question here is how to construct well formed XML! You are definitely helping me out!

Perhaps you can help me with the following exercise. I have generated a really nasty looking XML file by way of XML::Simple in Perl (dumping hash contents into XML). If I were to clean this up, how would it look? Also, where may I find a set of *understandable* rules about the correct formatting of XML? I have a third question, but I think it will be more appropriate for me to open another question for more points, which is: how do I link the dataset to different tools besides a DataGrid (like a label for instance), or link an image (a url) in the XML through the dataset to a PictureBox?

test case:

<opt current_player="0">
  <current_cards></current_cards>
  <dealer current_hand="0" number_hands="1" upcard="1D">
    <bank></bank>
    <hands name="table">
      <bucket_0>1D</bucket_0>
      <bucket_0>4D</bucket_0>
    </hands>
    <rules doubleAfterPairSplit="1" doublingNumCards="0" doublingRules="2" earlySurrender="0" hitSplitAce="1" insurance="1" lateSurrender="1" natural="2" numDecks="1" penetration="100" resplitAce="1" resplitPairs="1" soft17="0" softDoubling="1" splitAce="1" splitPairs="1" ties="0" />
  </dealer>
  <deck numDecks="1" penetration="100">
    <deck>13D</deck>
    <deck>3H</deck>
    <deck>1C</deck>
    <deck>3S</deck>
    <deck>8S</deck>
    <deck>10H</deck>
    <deck>11S</deck>
    <deck>8C</deck>
    <deck>6C</deck>
    <deck>2S</deck>
    <deck>5H</deck>
    <deck>2D</deck>
    <deck>9D</deck>
    <deck>10S</deck>
    <deck>13C</deck>
    <deck>9C</deck>
    <deck>5C</deck>
    <deck>11C</deck>
    <deck>12H</deck>
    <deck>11H</deck>
    <deck>10C</deck>
    <deck>7D</deck>
    <deck>1S</deck>
    <deck>4S</deck>
    <deck>7S</deck>
    <deck>11D</deck>
    <deck>5S</deck>
    <deck>8D</deck>
    <deck>2C</deck>
    <deck>8H</deck>
    <deck>12D</deck>
    <deck>3C</deck>
    <deck>6D</deck>
    <deck>1H</deck>
    <deck>4H</deck>
    <deck>6H</deck>
    <deck>13S</deck>
    <deck>4C</deck>
    <deck>12C</deck>
    <deck>12S</deck>
    <deck>7H</deck>
    <deck>9S</deck>
    <deck>5D</deck>
    <deck>3D</deck>
    <deck>10D</deck>
    <deck>7C</deck>
    <holeCard></holeCard>
  </deck>
  <legal upcard="1D">
    <rules doubleAfterPairSplit="1" doublingNumCards="0" doublingRules="2" earlySurrender="0" hitSplitAce="1" insurance="1" lateSurrender="1" natural="2" numDecks="1" penetration="100" resplitAce="1" resplitPairs="1" soft17="0" softDoubling="1" splitAce="1" splitPairs="1" ties="0" />
  </legal>
  <option_list></option_list>
  <players bank="1000" buyin="1000" current_hand="0" insurance="0" insurance_wager="50" natural_payout="1.5" number_hands="1" player_id="player 1" surrender="0" wager="100">
    <decisions name="table" />
    <hands name="table">
      <bucket_0>6S</bucket_0>
      <bucket_0>13H</bucket_0>
    </hands>
  </players>
  <players bank="500" buyin="500" current_hand="0" insurance="0" insurance_wager="50" natural_payout="1.5" number_hands="1" player_id="player 2" surrender="0" wager="100">
    <decisions name="table" />
    <hands name="table">
      <bucket_0>9H</bucket_0>
      <bucket_0>2H</bucket_0>
    </hands>
  </players>
  <rules doubleAfterPairSplit="1" doublingNumCards="0" doublingRules="2" earlySurrender="0" hitSplitAce="1" insurance="1" lateSurrender="1" natural="2" numDecks="1" penetration="100" resplitAce="1" resplitPairs="1" soft17="0" softDoubling="1" splitAce="1" splitPairs="1" ties="0" />
  <upcard></upcard>
</opt>


ok, i cleaned it up some more, this is tough to figure out

<opt current_player="0">
  <current_cards></current_cards>
  <dealer current_hand="0" number_hands="1">
    <bank></bank>
    <hands name="table" />
    <upcard></upcard>
  </dealer>
  <deck numDecks="1" penetration="100">
    <deck>5H</deck>
    <deck>12H</deck>
    <deck>3C</deck>
    <deck>4C</deck>
    <deck>12S</deck>
    <deck>7S</deck>
    <deck>4D</deck>
    <deck>1H</deck>
    <deck>11C</deck>
    <deck>9H</deck>
    <deck>1S</deck>
    <deck>5D</deck>
    <deck>6D</deck>
    <deck>1C</deck>
    <deck>2D</deck>
    <deck>7H</deck>
    <deck>4S</deck>
    <deck>8S</deck>
    <deck>13C</deck>
    <deck>4H</deck>
    <deck>7C</deck>
    <deck>2C</deck>
    <deck>5S</deck>
    <deck>3D</deck>
    <deck>8H</deck>
    <deck>1D</deck>
    <deck>10H</deck>
    <deck>13S</deck>
    <deck>9C</deck>
    <deck>3H</deck>
    <deck>6S</deck>
    <deck>10S</deck>
    <deck>2S</deck>
    <deck>11S</deck>
    <deck>9D</deck>
    <deck>9S</deck>
    <deck>7D</deck>
    <deck>6H</deck>
    <deck>11H</deck>
    <deck>6C</deck>
    <deck>2H</deck>
    <deck>13H</deck>
    <deck>12D</deck>
    <deck>5C</deck>
    <deck>3S</deck>
    <deck>11D</deck>
    <deck>10C</deck>
    <deck>10D</deck>
    <deck>13D</deck>
    <deck>8D</deck>
    <deck>8C</deck>
    <deck>12C</deck>
    <holeCard></holeCard>
  </deck>
  <legal>
    <upcard></upcard>
  </legal>
  <option_list></option_list>
  <players bank="5000" buyin="5000" current_hand="0" insurance="0" insurance_wager="50" natural_payout="1.5" number_hands="1" player_id="Tom" surrender="0" wager="100">
    <decisions name="table" />
    <hands name="table" />
  </players>
  <rules doubleAfterPairSplit="1" doublingNumCards="0" doublingRules="2" earlySurrender="0" hitSplitAce="1" insurance="1" lateSurrender="1" natural="2" numDecks="1" penetration="100" resplitAce="1" resplitPairs="1" soft17="0" softDoubling="1" splitAce="1" splitPairs="1" ties="0" />
  <upcard></upcard>
</opt>
If you want to test your xml file, just add a
Damned mouse :-),

 If you want to test your xml file, just add a new xml document to your project (add new item/xml document) and paste in the xml you have, if there is something wrong with it, the IDE will tell you what and where.
I don't know what the xml file should do, so I came up with this

<opt current_player="0">
      <current_cards></current_cards>
      <dealer current_hand="0" number_hands="1">
            <bank></bank>
            <hands name="table" />
            <upcard></upcard>
      </dealer>
      <deck numDecks="1" penetration="100">
            <card1>5H</card1>
            <card2>12H</card2>
            <card3>3C</card3>
            <card4>4C</card4>
            <card5>12S</card5>
            <card6>7S</card6>
            <card7>4D</card7>
            <card8>1H</card8>
            <card9>11C</card9>
            <card10>9H</card10>
            <card11>1S</card11>
            <card12>5D</card12>
            <card13>6D</card13>
            <card14>1C</card14>
            <card15>2D</card15>
            <card16>7H</card16>
            <card17>4S</card17>
            <card18>8S</card18>
            <card19>13C</card19>
            <card20>4H</card20>
            <card21>7C</card21>
            <card22>2C</card22>
            <card23>5S</card23>
            <card24>3D</card24>
            <card25>8H</card25>
            <card26>1D</card26>
            <card27>10H</card27>
            <card28>13S</card28>
            <card29>9C</card29>
            <card30>3H</card30>
            <card31>6S</card31>
            <card32>10S</card32>
            <card33>2S</card33>
            <card34>11S</card34>
            <card35>9D</card35>
            <card36>9S</card36>
            <card37>7D</card37>
            <card38>6H</card38>
            <card39>11H</card39>
            <card40>6C</card40>
            <card41>2H</card41>
            <card42>13H</card42>
            <card43>12D</card43>
            <card44>5C</card44>
            <card45>3S</card45>
            <card46>11D</card46>
            <card47>10C</card47>
            <card48>10D</card48>
            <card49>13D</card49>
            <card50>8D</card50>
            <card51>8C</card51>
            <card52>12C</card52>
            <holeCard></holeCard>
      </deck>
      <legal>
            <upcard></upcard>
      </legal>
      <option_list></option_list>
      <players bank="5000" buyin="5000" current_hand="0" insurance="0" insurance_wager="50" natural_payout="1.5"
            number_hands="1" player_id="Tom" surrender="0" wager="100">
            <decisions name="table" />
      </players>
      <rules doubleAfterPairSplit="1" doublingNumCards="0" doublingRules="2" earlySurrender="0"
            hitSplitAce="1" insurance="1" lateSurrender="1" natural="2" numDecks="1" penetration="100"
            resplitAce="1" resplitPairs="1" soft17="0" softDoubling="1" splitAce="1" splitPairs="1"
            ties="0" />
      <upcard></upcard>
</opt>