2
我想解析XML字符串,像这样的:解析XML得到节点值
<Folk id="4630" country="US" name="Marry" />
(这是摆在富文本编辑器)
并获取id
,country
,name
值。
什么有我想:
Dim content As String = Me.RichTextBox1.Text
Dim doc As New System.Xml.XmlDocument
Try
doc.LoadXml(content)
Catch ex As Exception
Label2.Text = "No XML Elements!!!!"
End Try
Dim items = doc.GetElementsByTagName("Folk")
For Each item As System.Xml.XmlElement In items
Dim id As String = item.ChildNodes(0).InnerText()
MsgBox(id) 'Try to prompt a message box containing the id=""
Next
它与一个错误显示出来结束了:NullReferenceException was unhandled.
- 它不发现id
那里,所以我不处理此错误,首先,我想获得正确的回报,那么我会处理,如果没有发现任何东西。那为什么它不返回Folk
id=""
?对节点的访问是否错误?
是的。你是对的,但是如果有多个' ',它只是读取第一个。 ': - /'但是至少它的工作...... –
Lucas
我想如果有很多' '条目,那么他们应该被正确包装在' '里,然后你会参考个别的'ChildNodes(x)'你最初的尝试方式。合理? –
语法会变成:'Dim id As String = item.ChildNodes(0).Attributes(0)' –