我已经仔细阅读了LINQ文档,并查看了以前关于堆栈溢出的一些答案,但我仍然对LINQ的工作原理感到困惑。我想从网站上获取一些数据,但我不知道如何将xml解析为字符串。以下是我目前掌握的情况:
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"?>
<game>
<id>
</id>
<venue>
</venue>
</game>
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
MessageBox.Show(strId)
End Sub
End Class
因此,当表单加载时,它会将XNamespace设置为ns,将XDocument设置为xml。当我单击表单上的cmdGetData按钮时,它应该将网站名称加载到XNamespace,然后获取第一个id元素的值并将其放入strId变量中。然后它应该在消息框中打印该值。我知道我做错了什么,但我不知道该怎么做才能解决它。