Home Scraping XML from a website with VB.NET and LINQ
Reply: 0

Scraping XML from a website with VB.NET and LINQ

user1789 Published in June 25, 2018, 7:51 am

I've been reading thorough the LINQ documentation and looking at some previous answers on Stack Overflow but I'm still pretty confused about how LINQ works. I want to grab some data from a website, but I can't figure out how to get the xml to parse into strings. Here is what I have so far:

Public Class Form1
    'Dim xml As XDocument
    Dim ns As XNamespace
    Dim strXMLSource As String = "http://gd2.mlb.com/components/game/mlb/year_2018/month_03/day_29/gid_2018_03_29_anamlb_oakmlb_1/linescore.xml"

    Dim xml As XDocument = <?xml version="1.0" encoding="utf-16"?>

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        txtXMLSource.Text = strXMLSource
    End Sub

    Private Sub cmdGetData_Click(sender As System.Object, e As System.EventArgs) Handles cmdGetData.Click
        ns = txtXMLSource.Text
        Dim strGame As XElement = xml.Descendants(ns + "game").First
        Dim strId As String = strGame.Descendants(ns + "id").First
    End Sub
End Class

So when the form loads it sets up an XNamespace as ns and an XDocument as xml. When I click the cmdGetData button on the form, it should load the website name to the XNamespace and then grab the value of the first id element and put it in the strId variable. And then it should print that value in a message box. I know I'm doing something wrong but I have no idea what to do to fix it.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.370684 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO