Linq xml - clause where

Résolu
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 - 24 sept. 2015 à 09:17
Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024 - 24 sept. 2015 à 20:32
Bonjour le Forum,

Je dispose d'un fichier xml dont l'arborescence est la suivante
 <Environments>
    <Environment name="exploitation" ....
      <Applications>
        <Application name="A-P9999H01" ....
          <Jobs>
            <Job name="P9999H0101" ....
            </Job>
            <Job name="P9999H0102" 
            </Job>
          </Jobs>
        </Application>
        <Application name="A-P9999D02" ....
          <Jobs>
            <Job name="P9999D0202" .... 
            </Job>
          </Jobs>
        </Application>
    </Environment>
    <Environment name="production" ....
      <Applications>
        <Application name="A-P1111M01" ....
        ....
        ....
    </Environment>
  </Environments>


La requête
Using reader As New StreamReader("monfichier.xml")
mydoc = XDocument.Load(reader)
End Using

Dim job = (From items In mydoc.Root.Descendants("Environment").Descendants("Application").Descendants("Job") _
Select items)

MessageBox.Show(CStr(job.Count))
me retourne correctement le nombre total de "job".

Je souhaite filtrer le nombre de job par environment.
La requête
Dim job = (From items In MyDoc.Root.Descendants("Environment").Descendants("Application").Descendants("Job") _
Where CStr(items.Elements("Environment").@name) = "exploitation"
Select items)
me retourne 0

Quelle est mon erreur ???
Merci de vos suggestions.

jean-marc

1 réponse

Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024 656
24 sept. 2015 à 13:07
Bonjour, un truc du genre

From items In mydoc.Root.Descendants("Environment").where(Function(env) env.Name = "exploitation"


Je n'ai pas de quoi tester sous la main.
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
24 sept. 2015 à 13:23
bonjour Whismeril,

Ta proposition me retourne 0.

J'ai testé, également, en vain
Dim job = From items In MyDoc.Root.Descendants("Environment").Descendants("Application").Descendants("Job").Where(Function(env) env.Name = "exploitation") 
me retourne 0

Je vais me créer un xml réduit pour tests.

Merci de ton intérêt,
jean-marc
0
Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024 656 > cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018
24 sept. 2015 à 13:50
Si tu veux bien poster tyon xml réduit, j'essayerai ce soir.
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27 > Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024
24 sept. 2015 à 14:44
Le xml réduit
<?xml version="1.0" encoding="UTF-8"?>
<Domain name="Test jean-marc">
  <Environments>
    <Environment name="exploitation">
      <Applications>
        <Application name="Appli1">
          <Jobs>
            <Job name="appli1_exploitation_Job1">
            </Job>
            <Job name="appli1_exploitation_Job2">
            </Job>
            <Job name="appli1_exploitation_Job3">
            </Job>
            <Job name="appli1_exploitation_Job4">
            </Job>
          </Jobs>
        </Application>
        <Application name="Appli2">
          <Jobs>
            <Job name="appli2_exploitation_Job5">
            </Job>
          </Jobs> 
        </Application>
      </Applications>		
    </Environment>
    <Environment name="production">
      <Applications>
        <Application name="Appli3">
          <Jobs>
            <Job name="appli3_production_Job1">
            </Job>
            <Job name="appli3_production_Job2">
            </Job>
          </Jobs>
        </Application>
        <Application name="Appli4">
          <Jobs>
            <Job name="appli4_production_Job3">
            </Job>
          </Jobs> 
        </Application>
      </Applications>		
    </Environment>	
  </Environments>
</Domain>


Il y 2 environnements (exploitation et production).
L'environnement "exploitation" contient 2 applications et 5 jobs.
L'environnement "production" contient 2 applications et 3 jobs.

        Using reader As New StreamReader(PathRoot & PathExportXML & "export_" & "Test" & ".xml")
            mydoc = XDocument.Load(reader)
        End Using

        Dim job = From items In mydoc.Descendants("Job") _
                  Select items
        MessageBox.Show(CStr(job.Count))
me retourne 8 jobs

Je souhaiterai, dans un premier temps, récupérer le nombre de jobs pour l'environnement "exploitation".
0
Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024 656
24 sept. 2015 à 17:54
        Dim maListe As IEnumerable = (
            From item In xDoc.Descendants("Environment").Where(Function(j) j.@name = "exploitation").Descendants("Job")
            Select item)


me retourne 5 item, je te laisse en faire des Jobs.

Par contre ce n'est pas la peine de mettre toute l'arborescence dans les Descendant(), va direct à celui que tu veux
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27 > Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024
24 sept. 2015 à 18:42
Merci Whismeril de ta réponse.
        Dim countjob As Integer = (
            From item In mydoc.Descendants("Environment").Where(Function(j) j.@name = "exploitation").Descendants("Job")
            Select item).Count

        MessageBox.Show("Nombre de job : " & countjob.ToString)
me retourne correctement le nombre de job pour l'environnement "exploitation".

Ton "Dim maListe As IEnumerable" m'a orienté vers le "Dim countjob As Integer" avec le Count.

Bonne fin de journée et bonne soirée.

jean-marc
0
Rejoignez-nous