2016-05-09 5 views
0

Ich muss alle Werte basierend auf untergeordneten Elementwert gruppieren. Im Folgenden finden Sie Beispiel meiner XMLASP.Net VB - Linq zu XML-Gruppe von Kind Element Wert

<Matchday date="2016-05-09"> 
    <Match id="1288348"> 
     <Home id="13" name="club1"/> 
     <Away id="14" name="club2"/> 
     <Information> 
     <country>England</country> 
     </Information> 
    </Match> 
    <Match id="1288349"> 
     <Home id="15" name="club3"/> 
     <Away id="16" name="club4"/> 
     <Information> 
     <country>England</country> 
     </Information> 
    </Match> 
    <Match id="1288350"> 
     <Home id="17" name="club5"/> 
     <Away id="18" name="club6"/> 
     <Information> 
     <country>Italy</country> 
     </Information> 
    </Match> 
    <Match id="1288351"> 
     <Home id="19" name="club7"/> 
     <Away id="20" name="club8"/> 
     <Information> 
     <country>Spain</country> 
     </Information> 
    </Match> 
</Matchday> 

ich nach Land gruppieren will, so wird das Ergebnis so aussehen:

England 
    1288348 
    1288349 

Italy 
    1288350 

Spain 
    1288351 

Wie dies mit vb Linq von Code-Behind zu XML-Abfrage zu tun.

Dank

Antwort

3

Dies ist eine mögliche Art und Weise:

Dim result = data.Elements("Match") _ 
       .GroupBy(Function(x) x.Element("Information").Element("country").Value) 

For Each r As IGrouping(Of String, XElement) In result 
    Console.WriteLine(r.Key) 
    For Each m As XElement In r 
     Console.WriteLine([email protected]) 
    Next 
Next 

wo data ist ein XElement wie folgt erklärt:

Dim data As XElement = <Matchday date="2016-05-09"> 
         <Match id="1288348"> 
          <Home id="13" name="club1"/> 
          <Away id="14" name="club2"/> 
          <Information> 
           <country>England</country> 
          </Information> 
         </Match> 
         <Match id="1288349"> 
          <Home id="15" name="club3"/> 
          <Away id="16" name="club4"/> 
          <Information> 
           <country>England</country> 
          </Information> 
         </Match> 
         <Match id="1288350"> 
          <Home id="17" name="club5"/> 
          <Away id="18" name="club6"/> 
          <Information> 
           <country>Italy</country> 
          </Information> 
         </Match> 
         <Match id="1288351"> 
          <Home id="19" name="club7"/> 
          <Away id="20" name="club8"/> 
          <Information> 
           <country>Spain</country> 
          </Information> 
         </Match> 
        </Matchday> 

und der Ausgang ist wie folgt:

England 
1288348 
1288349 
Italy 
1288350 
Spain 
1288351 
+0

danke, Sie Code funktioniert gut, aber ich musste Ihre Anfrage ein wenig beheben. Dies ist die Arbeitsabfrage: Dim matchGroup = Von mG In xe.Descendants.Elements ("Spieltag"). Elemente ("Match"). GroupBy (Funktion (x) x.Element ("Information"). Element ("Land")) .Wert) Wählen Sie mG – Robert

+0

Was ist 'xe' in Ihrem Code? Wenn es ein 'XDocument' ist, verwenden Sie' x.Root' anstelle von 'xe.Descendants()'. Ersteres wäre viel effizienter. – har07

+0

Ich habe das vergessen, Dim xe als XElement = XElement.Load (Server.MapPath ("exporter.xml")) – Robert