2017-10-17 82 views
0

所以,我的數組不是通過下一個元素,而不是循環它只是返回這一點。我在某處丟失了一個循環嗎?故障返回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 

................................... ................................................

+0

你能不只是返回Items.ToArray() –

+0

您可以詳細說明Items.ToArray()嗎?我對For Each Loop有一種感覺,但我可能是錯的。 – DJN

+0

不刪除for循環,只返回Items.ToArray()。看到我的答案。 –

回答

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 

乾杯

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 

在這裏使用匿名方法是結果的屏幕圖像

enter image description here

+0

你的結果是否會返回這樣的列表? MARRAY {長度= 21}字符串(,) (0,0) 「星期日」 字符串(0,1) 「12:00」 字符串(0,2) 「5:00」 字符串 (1,0)「星期一「字符串(1,1) 」10:00「 字符串(1,2) 」8:00「 字符串 (2,0) 」星期二「 字符串(2,1) 」10:00「 字符串(2,2) 「8:00」字符串 (3,0)「週三」字符串(3,1),「00」的字符串..等,如果沒有,你會怎麼選擇週二喜歡使用(2,0)的整數。 ToList?我問,因爲這是我的第一種方法,但需要能夠像數組一樣選擇它。 – DJN

+0

添加了結果圖像 – jdweng

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