【发布时间】:2013-04-10 18:14:36
【问题描述】:
我有一个 VB.net 程序。我正在尝试使用 XMLReader 读取 .xml 文件。我想将 XML 文件分解为不同的“部分”,在此示例中为 "FormTitle" 和 "ButtonTitle"。我想从FormTitle 中获取<Text> 数据并将其显示为"text" 表单并在"ButtonTitle" 中获取<Text> 并将其显示在按钮文本中。
这是我的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<!--XML Database.-->
<FormTitle>
<Text>Form Test</Text>
</FormTitle>
<ButtonTitle>
<Text>Button Test</Text>
</ButtonTitle>
这是我当前的代码:
If (IO.File.Exists("C:\testing.xml")) Then
Dim document As XmlReader = New XmlTextReader("C:\testing.xml")
While (document.Read())
Dim type = document.NodeType
If (type = XmlNodeType.Element) Then
'
If (document.Name = "Text") Then
Me.Text = document.ReadInnerXml.ToString()
End If
End If
End While
Else
MessageBox.Show("The filename you selected was not found.")
End If
如何在下一节中引入(ButtonTitle) 与FormTitle 中的同名,即(Text)。我假设我需要在 if then 语句中引用 FormTitle 和 ButtonTitle?
【问题讨论】:
-
请注意,您的 XML 文件格式不正确。 XML 文件应该始终有一个根元素,而您的则有两个 - FormTitle 和 ButtonTitle。也许您应该将它们包装在父元素中,也许是
.