所以,我的数组没有循环遍历下一个元素,而只是返回它。我错过了某个地方的循环?
以下是 mArray 现在返回:
mArray {Length=21} String(,)
(0,0) "Saturday" String
(0,1) "12:00" String
(0,2) "5:00" String
(1,0) "Saturday" String
(1,1) "12:00" String
(1,2) "5:00" String
(2,0) "Saturday" String
(2,1) "12:00" String
(2,2) "5:00" String
(3,0) "Saturday" String
(3,1) "12:00" String
(3,2) "5:00" String
(4,0) "Saturday" String
(4,1) "12:00" String
(4,2) "5:00" String
(5,0) "Saturday" String
(5,1) "12:00" String
(5,2) "5:00" String
(6,0) "Saturday" String
(6,1) "12:00" String
(6,2) "5:00" String
这是我想要的结果:
mArray {Length=21} String(,)
(0,0) "Sunday" String
(0,1) "12:00" String
(0,2) "5:00" String
(1,0) "Monday" String
(1,1) "10:00" String
(1,2) "8:00" String
(2,0) "Tuesday" String
(2,1) "10:00" String
(2,2) "8:00" String
(3,0) "Wednesday" String
(3,1) "10:00" String
(3,2) "8:00" String
(4,0) "Thursday" String
(4,1) "10:00" String
(4,2) "6:00" String
(5,0) "Friday" String
(5,1) "10:00" String
(5,2) "6:00" String
(6,0) "Saturday" String
(6,1) "12:00" String
(6,2) "5:00" String
XML文件供参考
<BranchHours>
<Hours>
<DayOfWeek>Sunday</DayOfWeek>
<Open>12:00</Open>
<Close>5:00</Close>
</Hours>
<Hours>
<DayOfWeek>Monday</DayOfWeek>
<Open>10:00</Open>
<Close>8:00</Close>
</Hours>
<Hours>
<DayOfWeek>Tuesday</DayOfWeek>
<Open>10:00</Open>
<Close>8:00</Close>
</Hours>
<Hours>
<DayOfWeek>Wednesday</DayOfWeek>
<Open>10:00</Open>
<Close>8:00</Close>
</Hours>
<Hours>
<DayOfWeek>Thursday</DayOfWeek>
<Open>10:00</Open>
<Close>6:00</Close>
</Hours>
<Hours>
<DayOfWeek>Friday</DayOfWeek>
<Open>10:00</Open>
<Close>6:00</Close>
</Hours>
<Hours>
<DayOfWeek>Saturday</DayOfWeek>
<Open>12:00</Open>
<Close>5:00</Close>
</Hours>
</BranchHours>
以下是功能:
Public Shared Function BranchOpenClose(ByVal branchCode As String) As Array
'set XML URL path
Dim URLString As String = "url/branchesTesting.xml"
'load URL Path
Dim xmlDoc As XDocument = XDocument.Load(URLString)
'decalre a 2- dimensional array of string as:
Dim mArray(6, 2) As String
Dim i As Integer
' Dim j As String
'Find XML Path Using Passed BranchCode variable & Select Hours Element Values
Dim Items = From BranchHours In xmlDoc.XPathSelectElements("/BranchesInfo/BranchInfo[BranchId='" & branchCode & "']/BranchHours/Hours") _
Select DayOfWeek = ((BranchHours.Elements("DayOfWeek").ToArray.Value)), _
Open = ((BranchHours.Elements("Open").ToArray.Value)), _
Close = ((BranchHours.Elements("Close").ToArray.Value))
For Each Hours In Items
For i = 0 To 6
mArray(i, 0) = Hours.DayOfWeek
mArray(i, 1) = Hours.Open
mArray(i, 2) = Hours.Close
Next
Next
Return mArray
End Function
............................................... ....................................
答案 0 :(得分:0)
对xml linq使用匿名方法
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Dim FILENAME As String = "c:\temp\test.xml"
Sub Main()
Dim doc As XDocument = XDocument.Load(FILENAME)
Dim results = doc.Descendants("Hours").Select(Function(x) New With { _
.dayOfWeek = x.Element("DayOfWeek").Value, _
.open = CType(x.Element("Open"), DateTime),
.close = CType(x.Element("Close"), DateTime)
}).ToList()
End Sub
End Module
以下是结果的屏幕图像
答案 1 :(得分:0)
这是我正在寻找的解决方案。 我在另一个循环中有一个循环它经过每小时,然后从0到6再次....我只需要一个简单的计数器而不是额外的FOR。 这就是为什么它一遍又一遍重写我的价值观
For Each Hours In Items
mArray(i, 0) = Hours.DayOfWeek
mArray(i, 1) = Hours.Open
mArray(i, 2) = Hours.Close
i+=1
Next
干杯
答案 2 :(得分:0)
您不需要自己填充数组,使用ToArray():
Public Shared Function BranchOpenClose(ByVal branchCode As String) As Array
'set XML URL path
Dim URLString As String = "url/branchesTesting.xml"
'load URL Path
Dim xmlDoc As XDocument = XDocument.Load(URLString)
Dim i As Integer
'Find XML Path Using Passed BranchCode variable & Select Hours Element Values
Dim Items = From BranchHours In xmlDoc.XPathSelectElements("/BranchesInfo/BranchInfo[BranchId='" & branchCode & "']/BranchHours/Hours") _
Select DayOfWeek = ((BranchHours.Elements("DayOfWeek").ToArray.Value)), _
Open = ((BranchHours.Elements("Open").ToArray.Value)), _
Close = ((BranchHours.Elements("Close").ToArray.Value))
Return Items.ToArray()
End Function