16,555
社区成员
发帖
与我相关
我的任务
分享
Sub GetListByXPath()
Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.Load("D:\VisualStudioProject2008\Test\LinqToXml\data.xml")
Dim List2 As List(Of Hashtable) = New List(Of Hashtable)
Dim nodes As XmlNodeList = xmlDoc.SelectNodes("//LNshop")
For Each node As XmlNode In nodes
Dim rec As Hashtable = New Hashtable
rec.Add("provinceName", node.SelectSingleNode("ancestor::province").Attributes("provinceName").Value)
rec.Add("cityName", node.SelectSingleNode("ancestor::city").Attributes("cityName").Value)
rec.Add("areaName", node.SelectSingleNode("ancestor::area").Attributes("areaName").Value)
rec.Add("shopName", node.Attributes("shopName").Value)
rec.Add("address", node.Attributes("address").Value)
List2.Add(rec)
Next
For Each s As Hashtable In List2
Console.WriteLine("{0}, {1}, {2}, {3}, {4}", s("provinceName"), s("cityName"), s("areaName"), s("shopName"), s("address"))
Next
Console.Read()
End Sub
Sub Main()
Dim xmlDoc As XDocument = XDocument.Load("D:\VisualStudioProject2008\Test\LinqToXml\data.xml")
Dim List1 = (From el In xmlDoc.<shop>.<province>.<city>.<area>.Elements _
Select el.Ancestors("province").@provinceName, _
el.Ancestors("city").@cityName, _
el.Ancestors("area").@areaName, _
el.@shopName, _
el.@address).ToList()
For Each s In List1
Console.WriteLine("{0}, {1}, {2}, {3}, {4}", s.provinceName, s.cityName, s.areaName, s.shopName, s.address)
Next
Console.Read()
End Sub
'创建DataTable用来存取所要的数据
Dim dt As New DataTable()
Dim col As New DataColumn("Province", GetType(String))
dt.Columns.Add(col)
col = New DataColumn("City", GetType(String))
dt.Columns.Add(col)
col = New DataColumn("Area", GetType(String))
dt.Columns.Add(col)
col = New DataColumn("ShopName", GetType(String))
dt.Columns.Add(col)
col = New DataColumn("ShopAddress", GetType(String))
dt.Columns.Add(col)
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("D:\XMLFile1.xml")
Dim nodeList As XmlNodeList = xmlDoc.SelectSingleNode("shop").ChildNodes
'获取根节点的所有子节点
For Each provinceList As XmlNode In nodeList
'遍历所有province节点
For Each cityList As XmlNode In provinceList
'遍历所有city节点
For Each areaList As XmlNode In cityList
'遍历所有area节点
For Each shopList As XmlNode In areaList
'遍历所有LNshop节点
Dim dr As DataRow = dt.NewRow()
dr("Province") = provinceList.Attributes("provinceName").Value
dr("City") = cityList.Attributes("cityName").Value
dr("Area") = areaList.Attributes("areaName").Value
dr("ShopName") = shopList.Attributes("shopName").Value
dr("ShopAddress") = shopList.Attributes("address").Value
dt.Rows.Add(dr)
Next
Next
Next
Next