首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

Visual basic 2005怎么读取,查询xml文档

2012-01-09 
Visual basic 2005如何读取,查询xml文档?xml文档内容如下:?xmlversion 1.0 encoding GB2312 standa

Visual basic 2005如何读取,查询xml文档?
xml文档内容如下:
<?xml   version= "1.0 "   encoding= "GB2312 "   standalone= "yes "?>
<AreaCodeConf>
<state   Name= '北京 '>
<City   Name= '北京 '   AreaCode= '1000 '>
<Branch   Name= '新世界 '   DispatchNo= '205 '/>
<Branch   Name= '左安门 '   DispatchNo= '206 '/>
</City>
</state>
<state   Name= '广东省 '>
<City   Name= '东莞 '   AreaCode= '1001 '>
<Branch   Name= '凤岗办事处 '   DispatchNo= '207 '/>
<Branch   Name= '常平办事处 '   DispatchNo= '208 '/>
</City>
<City   Name= '广州 '   AreaCode= '1002 '>
<Branch   Name= '海珠办事处 '   DispatchNo= '209 '/>
<Branch   Name= '东山办事处 '   DispatchNo= '210 '/>
</City>
</state>
</AreaCodeConf>

现在想读取此文档并由DispatchNo的值比如说是209知道是广东省广州海珠办事处,怎么写程序?

[解决办法]
' 把xml存为d盘根目录的a.xml
' 程序不健壮,还需要完善

Imports System.Xml

Public Class Form1

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xd As New XmlDocument
xd.Load( "D:\a.xml ")
Dim nodes As XmlNodeList = xd.GetElementsByTagName( "Branch ")
Dim node As XmlNode
For i As Int32 = 0 To nodes.Count - 1
If nodes.ItemOf(i).Attributes.ItemOf(1).Value = TextBox1.Text Then
node = nodes.ItemOf(i)
End If
Next
Dim strBrach As String = node.Attributes.ItemOf(0).Value.ToString
Dim strCity As String = node.ParentNode.Attributes.ItemOf(0).Value.ToString & " "
Dim strProvince As String = node.ParentNode.ParentNode.Attributes.ItemOf(0).Value.ToString & " "
Dim s As String = strProvince & strCity & strBrach
MsgBox(s)
End Sub
End Class

热点排行