Entonces, mi matriz no está recorriendo el siguiente elemento, sino que solo está devolviendo esto. ¿Me falta un bucle en alguna parte?

Esto es lo que mArray regresa ahora:

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

Estos son mis resultados deseados:

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

Archivo XML para referencia

 <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>

Aquí está la función :

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
DJN 17 oct. 2017 a las 18:43

3 respuestas

La mejor respuesta

Esta es la solución que estaba buscando. Tenía un bucle dentro de otro bucle que atraviesa cada hora, y luego vuelve de 0 a 6 ... Solo necesitaba un contador simple aquí en lugar del FOR adicional. Por eso reescribió mis valores una y otra vez

   For Each Hours In Items

                mArray(i, 0) = Hours.DayOfWeek
                mArray(i, 1) = Hours.Open
                mArray(i, 2) = Hours.Close

                i+=1

        Next

Salud

0
DJN 17 oct. 2017 a las 22:23

No necesita llenar la matriz usted mismo, use 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
0
Steve Ford 18 oct. 2017 a las 08:34

Utilice el método anónimo con 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

Aquí está la imagen de la pantalla de resultados

enter image description here

0
jdweng 19 oct. 2017 a las 01:04