How to populate a VB Windows form Grid with an xml document(Data source)

I am working with VB .Net for windows forms applications. Not web applications. What I need to do is to the out of the box VB.net Grid control with an XML data source. The xml data source is just an xml file that will equate to two columns of data in the GridView.
Now I can find plenty of examples on the web which will populate a .NET Grid for a web application, but not for a desktop windows form application. Can anyone tell me how to do this?
LVL 2
brgdotnetcontractorAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
Hi brgdotnet;

Here is an example of using an XML document to Fill a DataGridView.

Test XML Document
<?xml version='1.0' encoding='UTF-8' ?>
<books>
  <book title="Windows Forms in Action">
    <publisher>Manning</publisher>
  </book>
  <book title="RSS and Atom in Action">
    <publisher>Manning</publisher>
  </book>
  <book title="Understanding and Using C Pointers">
    <publisher> O'Reilly</publisher>
  </book>
</books>

Open in new window

Using this VB code
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
     '' Change the path and file name to match where you loaded the document
     Dim xdoc As XDocument = XDocument.Load("C:\Working Directory\Books.xml")

     Dim books = (From book In xdoc.Root.Descendants("book")
                  Select New With
                  {
                      .Title = book.Attribute("title").Value,
                      .Publisher = book.Element("publisher").Value
                  }).ToList()

     DataGridView1.DataSource = books

End Sub

Open in new window

Will result in this.
DataGridView Results
0
 
Naitik GamitSoftware DeveloperCommented:
Look at this microsoft support blog for: Read XML Data into a DataSet by Using Visual Basic .NET

https://support.microsoft.com/en-us/kb/309702
0
 
brgdotnetcontractorAuthor Commented:
Thank you Fernando
0
 
Fernando SotoRetiredCommented:
Not a problem brgdotnet, glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.