考虑到我有以下XML文件:
<family>
<name gender="Male">
<firstname>Tom</firstname>
<lastname>Smith</lastname>
<phone>111111</phone>
<phone>222222</phone>
</name>
<name gender="Female">
<firstname>Dale</firstname>
<lastname>Smith</lastname>
<phone>111111</phone>
<phone>222222</phone>
</name>
</family>
我使用XmlTextReader来解析XML,我的代码是这样的:
Dim m_xmlr As XmlTextReader
m_xmlr = New XmlTextReader(InputXML)
m_xmlr.WhitespaceHandling = WhitespaceHandling.None
m_xmlr.Read()
m_xmlr.Read()
While Not m_xmlr.EOF
m_xmlr.Read()
If Not m_xmlr.IsStartElement() Then
Exit While
End If
Dim gender as string = m_xmlr.GetAttribute("gender")
m_xmlr.Read()
Dim FirstName As Array = m_xmlr.ReadElementString("firstname")
Dim LastName As String = m_xmlr.ReadElementString("lastname")
Dim phone As String = m_xmlr.ReadElementString("phone")
End While
m_xmlr.Close()
我的问题是,如何让两个手机节点放入2个不同的变量?
PS:我无法修改xml,也没有任何att可以添加到任何节点。
提前谢谢。
答案 0 :(得分:1)
您可以在第一个之后添加另一个ReadElementString
:
Dim phone1 As String = m_xmlr.ReadElementString("phone")
Dim phone2 As String = m_xmlr.ReadElementString("phone")
注意:为了让你的样品工作,我不得不做一些小修改,即
While Not m_xmlr.EOF
Dim gender As String = m_xmlr.GetAttribute("gender")
m_xmlr.Read()
Dim FirstName As String = m_xmlr.ReadElementString("firstname")
Dim LastName As String = m_xmlr.ReadElementString("lastname")
Dim phone1 As String = m_xmlr.ReadElementString("phone")
Dim phone2 As String = m_xmlr.ReadElementString("phone")
End While