2017-10-17 22 views
0

Donc, mon tableau ne passe pas en boucle dans l'élément suivant au lieu de le renvoyer. Suis-je absent d'une boucle quelque part?Problème renvoyant le XML à la matrice 2D non en boucle vers l'élément suivant

Voici ce Marray Retour maintenant:

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 

Voici mes résultats souhaités:

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 

fichier XML Pour référence

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

Voici la Fonction:

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

Pouvez-vous pas seulement return Items.ToArray() –

+0

Pouvez-vous élaborer sur Items.ToArray()? J'ai le sentiment que c'est dans le For Each Loop mais je peux me tromper. – DJN

+0

ne supprime pas la boucle for et renvoie simplement Items.ToArray(). Vois ma réponse. –

Répondre

0

C'est la solution que je cherchais. J'avais une boucle à l'intérieur d'une autre boucle qu'elle traverse toutes les heures, puis je reviens de 0 à 6 ... J'avais juste besoin d'un simple compteur ici au lieu du FOR additionnel. Ce est pourquoi il a réécrit mes valeurs encore et encore

For Each Hours In Items 

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

       i+=1 

     Next 

Vive

0

Utilisez la méthode anonyme avec 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 

est ici image à l'écran des résultats

enter image description here

+0

Vos résultats retournent une liste pareil? mArray {Length = 21} Chaîne (,) (0,0) "Dimanche" Chaîne (0,1) "12:00" Chaîne (0,2) "5:00" Chaîne (1,0) "Lundi "String (1,1)" 10:00 "Chaîne (1,2)" 8:00 "Chaîne (2,0)" Mardi "Chaîne (2,1)" 10:00 "Chaîne (2,2) "8:00" Chaîne (3,0) "Mercredi" Chaîne (3,1) "10:00" Chaîne .. etc Si non, comment choisiriez-vous mardi avec seulement des entiers comme (2,0) en utilisant. Lister? Je pose la question parce que c'était ma première approche mais que je devais pouvoir la sélectionner comme un tableau. – DJN

+0

Ajout de l'image des résultats – jdweng

0

Vous n'êtes pas obligé de remplir le tableau vous, utilisez 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