Any way I can populate a datagrid in vb.net with the ff xml data. `

<?xml version="1.0" encoding="utf-8"?>
<pricelist>
<item>
<symbol>7UP</symbol>
<security>7-UP BOTTLING PLC</security>
<opening price>41.75</opening price>
<closing price>41.75</closing price>
<description>Symbol:7UP, Security:7-UP BOTTLING PLC, Opening price:41.75, Closing price:41.75</description>
<pubDate>Sun, 15 Apr 2012 01:00:10 GMT</pubDate>
</item>
<item>
<symbol>AGLEVENT</symbol>
<security>A.G.LEVENTIS(NIGERIA) PLC</security>
<opening price>1.44</opening price>
<closing price>1.44</closing price>
<description>Symbol:AGLEVENT, Security:A.G.LEVENTIS(NIGERIA) PLC, Opening price:1.44, Closing price:1.44</description>
<pubDate>Sun, 15 Apr 2012 01:00:10 GMT</pubDate>
</item>
</pricelist>

any help will be appreciated. Thank you

I would do it like this:

Imports System
Imports System.Collections
Imports System.Xml.Linq

Public Class Form1
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Dim xd = XDocument.Load("..\..\XMLFile1.xml")

      DataGridView1.DataSource =
      (
         From x As XElement In xd.Descendants("pricelist").Nodes()
         Select New With
         {
            .SYMBOL = x.Element("symbol").Value,
            .SECURITY = x.Element("security").Value,
            .OPENING_PRICE = x.Element("opening_price").Value,
            .CLOSING_PRICE = x.Element("closing_price").Value,
            .DESCRIPTION = x.Element("description").Value,
            .PUB_DATE = x.Element("pubDate").Value
         }
      ).ToList()
   End Sub
End Class

I did need to modify the XML changing "opening price" to "opening_price" and "closing price" to "closing_price".
I also removed the empty ="" from both.

Edited 4 Years Ago by thines01

thanks, that was helpful, but what do you think i should do if the website I am getting the data from shows it's xml nodes as "opening price" and "closing price" in caseyou found a way to let the system bypass this it will be helpful

You can do a replace on "opening price" changing it to "opening_price" before you pass the entire structure to the XML parser.

You can also try it WITH the space and see what happens.
I just noticed when I pasted it into a Visual Studio .xml file, it showed me errors in those spots, so I changed them.

the system automatically downloads the xml file from the website so how can I do that

First, try it with the spaces.
...but change my sample code (changing the underscore to a space the space when querying the name).

so far it only works with no space and I have talked to my contact and I will keep it that way

This article has been dead for over six months. Start a new discussion instead.