所以,我的數組不是通過下一個元素,而不是循環它只是返回這一點。我在某處丟失了一個循環嗎?故障返回XML到二維數組不循環到下一個元素
這裏是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
................................... ................................................
你能不只是返回Items.ToArray() –
您可以詳細說明Items.ToArray()嗎?我對For Each Loop有一種感覺,但我可能是錯的。 – DJN
不刪除for循環,只返回Items.ToArray()。看到我的答案。 –