IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

[DataBinding][XML] Explorer un fichier xml dans un Treeview


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre émérite
    Avatar de FRED.G
    Profil pro
    Inscrit en
    Novembre 2002
    Messages
    1 032
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France

    Informations forums :
    Inscription : Novembre 2002
    Messages : 1 032
    Par défaut [DataBinding][XML] Explorer un fichier xml dans un Treeview
    Hello,

    je n'arrive pas à lier les éléments d'un fichier xml à un Treeview. Je vouvrais reproduire toute la hiérarchie en affichant :
    • le nom de tous les éléments
    • le nom et la valeur des attributs de chaque éléments


    J'ai essayé de définir les ressources suivantes, mais je galère...
    Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
        <Window.Resources>
     
            <HierarchicalDataTemplate DataType="Node" ItemsSource="{Binding XPath=*}">
                <TextBlock Text="{Binding XPath=@Name}"/>
            </HierarchicalDataTemplate>
     
            <DataTemplate DataType="ChildNode">
                <TextBlock Text="{Binding XPath=@Name}"/>
            </DataTemplate>
     
            <XmlDataProvider x:Key="XmlNodeList" 
                             Source="C:\Users\FRED\Desktop\Northwind.edmx"/>
     
        </Window.Resources>
    Le fichier source est un fichier .edmx (fourni dans le zip en PJ) :
    Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    <?xml version="1.0" encoding="utf-8"?>
    <edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
      <edmx:Runtime>
        <!-- SSDL content -->
        <edmx:StorageModels>
        <Schema Namespace="NorthwindModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
            <EntityContainer Name="NorthwindModelStoreContainer">
              <EntitySet Name="Categories" EntityType="NorthwindModel.Store.Categories" store:Type="Tables" Schema="dbo" />
              <EntitySet Name="Customers" EntityType="NorthwindModel.Store.Customers" store:Type="Tables" Schema="dbo" />
              <EntitySet Name="Orders" EntityType="NorthwindModel.Store.Orders" store:Type="Tables" Schema="dbo" />
              <AssociationSet Name="FK_Orders_Customers" Association="NorthwindModel.Store.FK_Orders_Customers">
                <End Role="Customers" EntitySet="Customers" />
                <End Role="Orders" EntitySet="Orders" />
              </AssociationSet>
            </EntityContainer>
            <EntityType Name="Categories">
              <Key>
                <PropertyRef Name="CategoryID" />
              </Key>
              <Property Name="CategoryID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
              <Property Name="CategoryName" Type="nvarchar" Nullable="false" MaxLength="15" />
              <Property Name="Description" Type="ntext" />
              <Property Name="Picture" Type="image" />
            </EntityType>
            <EntityType Name="Customers">
              <Key>
                <PropertyRef Name="CustomerID" />
              </Key>
              <Property Name="CustomerID" Type="nchar" Nullable="false" MaxLength="5" />
              <Property Name="CompanyName" Type="nvarchar" Nullable="false" MaxLength="40" />
              <Property Name="ContactName" Type="nvarchar" MaxLength="30" />
              <Property Name="ContactTitle" Type="nvarchar" MaxLength="30" />
              <Property Name="Address" Type="nvarchar" MaxLength="60" />
              <Property Name="City" Type="nvarchar" MaxLength="15" />
              <Property Name="Region" Type="nvarchar" MaxLength="15" />
              <Property Name="PostalCode" Type="nvarchar" MaxLength="10" />
              <Property Name="Country" Type="nvarchar" MaxLength="15" />
              <Property Name="Phone" Type="nvarchar" MaxLength="24" />
              <Property Name="Fax" Type="nvarchar" MaxLength="24" />
            </EntityType>
            <EntityType Name="Orders">
              <Key>
                <PropertyRef Name="OrderID" />
              </Key>
              <Property Name="OrderID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
              <Property Name="CustomerID" Type="nchar" MaxLength="5" />
              <Property Name="EmployeeID" Type="int" />
              <Property Name="OrderDate" Type="datetime" />
              <Property Name="RequiredDate" Type="datetime" />
              <Property Name="ShippedDate" Type="datetime" />
              <Property Name="ShipVia" Type="int" />
              <Property Name="Freight" Type="money" />
              <Property Name="ShipName" Type="nvarchar" MaxLength="40" />
              <Property Name="ShipAddress" Type="nvarchar" MaxLength="60" />
              <Property Name="ShipCity" Type="nvarchar" MaxLength="15" />
              <Property Name="ShipRegion" Type="nvarchar" MaxLength="15" />
              <Property Name="ShipPostalCode" Type="nvarchar" MaxLength="10" />
              <Property Name="ShipCountry" Type="nvarchar" MaxLength="15" />
            </EntityType>
            <Association Name="FK_Orders_Customers">
              <End Role="Customers" Type="NorthwindModel.Store.Customers" Multiplicity="0..1" />
              <End Role="Orders" Type="NorthwindModel.Store.Orders" Multiplicity="*" />
              <ReferentialConstraint>
                <Principal Role="Customers">
                  <PropertyRef Name="CustomerID" />
                </Principal>
                <Dependent Role="Orders">
                  <PropertyRef Name="CustomerID" />
                </Dependent>
              </ReferentialConstraint>
            </Association>
          </Schema></edmx:StorageModels>
        <!-- CSDL content -->
        <edmx:ConceptualModels>
          <Schema Namespace="NorthwindModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
            <EntityContainer Name="NorthwindEntities">
              <EntitySet Name="Customers" EntityType="NorthwindModel.Customers" />
              <EntitySet Name="Orders" EntityType="NorthwindModel.Orders" />
              <AssociationSet Name="FK_Orders_Customers" Association="NorthwindModel.FK_Orders_Customers">
                <End Role="Customers" EntitySet="Customers" />
                <End Role="Orders" EntitySet="Orders" />
              </AssociationSet>
              </EntityContainer>
            <EntityType Name="Customers">
              <Key>
                <PropertyRef Name="CustomerID" />
              </Key>
              <Property Name="CustomerID" Type="String" Nullable="false" MaxLength="5" Unicode="true" FixedLength="true" />
              <Property Name="CompanyName" Type="String" Nullable="false" MaxLength="40" Unicode="true" FixedLength="false" />
              <Property Name="ContactName" Type="String" MaxLength="30" Unicode="true" FixedLength="false" />
              <Property Name="ContactTitle" Type="String" MaxLength="30" Unicode="true" FixedLength="false" />
              <Property Name="Address" Type="String" MaxLength="60" Unicode="true" FixedLength="false" />
              <Property Name="City" Type="String" MaxLength="15" Unicode="true" FixedLength="false" />
              <Property Name="Region" Type="String" MaxLength="15" Unicode="true" FixedLength="false" />
              <Property Name="PostalCode" Type="String" MaxLength="10" Unicode="true" FixedLength="false" />
              <Property Name="Country" Type="String" MaxLength="15" Unicode="true" FixedLength="false" />
              <Property Name="Phone" Type="String" MaxLength="24" Unicode="true" FixedLength="false" />
              <Property Name="Fax" Type="String" MaxLength="24" Unicode="true" FixedLength="false" />
              <NavigationProperty Name="Orders" Relationship="NorthwindModel.FK_Orders_Customers" FromRole="Customers" ToRole="Orders" />
            </EntityType>
            <EntityType Name="Orders">
              <Key>
                <PropertyRef Name="OrderID" />
              </Key>
              <Property Name="OrderID" Type="Int32" Nullable="false" />
              <Property Name="EmployeeID" Type="Int32" />
              <Property Name="OrderDate" Type="DateTime" />
              <Property Name="RequiredDate" Type="DateTime" />
              <Property Name="ShippedDate" Type="DateTime" />
              <Property Name="ShipVia" Type="Int32" />
              <Property Name="Freight" Type="Decimal" Precision="19" Scale="4" />
              <Property Name="ShipName" Type="String" MaxLength="40" Unicode="true" FixedLength="false" />
              <Property Name="ShipAddress" Type="String" MaxLength="60" Unicode="true" FixedLength="false" />
              <Property Name="ShipCity" Type="String" MaxLength="15" Unicode="true" FixedLength="false" />
              <Property Name="ShipRegion" Type="String" MaxLength="15" Unicode="true" FixedLength="false" />
              <Property Name="ShipPostalCode" Type="String" MaxLength="10" Unicode="true" FixedLength="false" />
              <Property Name="ShipCountry" Type="String" MaxLength="15" Unicode="true" FixedLength="false" />
              <NavigationProperty Name="Customers" Relationship="NorthwindModel.FK_Orders_Customers" FromRole="Orders" ToRole="Customers" />
            </EntityType>
            <Association Name="FK_Orders_Customers">
              <End Role="Customers" Type="NorthwindModel.Customers" Multiplicity="0..1" />
              <End Role="Orders" Type="NorthwindModel.Orders" Multiplicity="*" />
            </Association>
            </Schema>
        </edmx:ConceptualModels>
        <!-- C-S mapping content -->
        <edmx:Mappings>
          <Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
            <EntityContainerMapping StorageEntityContainer="NorthwindModelStoreContainer" CdmEntityContainer="NorthwindEntities">
              <EntitySetMapping Name="Customers">
                <EntityTypeMapping TypeName="IsTypeOf(NorthwindModel.Customers)">
                  <MappingFragment StoreEntitySet="Customers">
                    <ScalarProperty Name="CustomerID" ColumnName="CustomerID" />
                    <ScalarProperty Name="CompanyName" ColumnName="CompanyName" />
                    <ScalarProperty Name="ContactName" ColumnName="ContactName" />
                    <ScalarProperty Name="ContactTitle" ColumnName="ContactTitle" />
                    <ScalarProperty Name="Address" ColumnName="Address" />
                    <ScalarProperty Name="City" ColumnName="City" />
                    <ScalarProperty Name="Region" ColumnName="Region" />
                    <ScalarProperty Name="PostalCode" ColumnName="PostalCode" />
                    <ScalarProperty Name="Country" ColumnName="Country" />
                    <ScalarProperty Name="Phone" ColumnName="Phone" />
                    <ScalarProperty Name="Fax" ColumnName="Fax" />
                  </MappingFragment>
                </EntityTypeMapping>
              </EntitySetMapping>
              <EntitySetMapping Name="Orders">
                <EntityTypeMapping TypeName="IsTypeOf(NorthwindModel.Orders)">
                  <MappingFragment StoreEntitySet="Orders">
                    <ScalarProperty Name="OrderID" ColumnName="OrderID" />
                    <ScalarProperty Name="EmployeeID" ColumnName="EmployeeID" />
                    <ScalarProperty Name="OrderDate" ColumnName="OrderDate" />
                    <ScalarProperty Name="RequiredDate" ColumnName="RequiredDate" />
                    <ScalarProperty Name="ShippedDate" ColumnName="ShippedDate" />
                    <ScalarProperty Name="ShipVia" ColumnName="ShipVia" />
                    <ScalarProperty Name="Freight" ColumnName="Freight" />
                    <ScalarProperty Name="ShipName" ColumnName="ShipName" />
                    <ScalarProperty Name="ShipAddress" ColumnName="ShipAddress" />
                    <ScalarProperty Name="ShipCity" ColumnName="ShipCity" />
                    <ScalarProperty Name="ShipRegion" ColumnName="ShipRegion" />
                    <ScalarProperty Name="ShipPostalCode" ColumnName="ShipPostalCode" />
                    <ScalarProperty Name="ShipCountry" ColumnName="ShipCountry" />
                  </MappingFragment>
                </EntityTypeMapping>
              </EntitySetMapping>
              <AssociationSetMapping Name="FK_Orders_Customers" TypeName="NorthwindModel.FK_Orders_Customers" StoreEntitySet="Orders">
                <EndProperty Name="Customers">
                  <ScalarProperty Name="CustomerID" ColumnName="CustomerID" />
                </EndProperty>
                <EndProperty Name="Orders">
                  <ScalarProperty Name="OrderID" ColumnName="OrderID" />
                </EndProperty>
                <Condition ColumnName="CustomerID" IsNull="false" />
              </AssociationSetMapping>
              </EntityContainerMapping>
          </Mapping>
        </edmx:Mappings>
      </edmx:Runtime>
      <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
        <edmx:Connection>
          <DesignerInfoPropertySet>
            <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
          </DesignerInfoPropertySet>
        </edmx:Connection>
        <edmx:Options>
          <DesignerInfoPropertySet>
            <DesignerProperty Name="ValidateOnBuild" Value="true" />
          </DesignerInfoPropertySet>
        </edmx:Options>
        <edmx:Diagrams >
    <Diagram Name="Northwind">
    <EntityTypeShape EntityType="NorthwindModel.Customers" PointX="0.75" PointY="1.25" Width="1.5" Height="3.3263964843749996" IsExpanded="true" />
    <EntityTypeShape EntityType="NorthwindModel.Orders" PointX="3" PointY="1" Width="1.5" Height="3.7109993489583331" IsExpanded="true" />
    <AssociationConnector Association="NorthwindModel.FK_Orders_Customers" ManuallyRouted="false">
    <ConnectorPoint PointX="2.25" PointY="2.9131982421875" />
    <ConnectorPoint PointX="3" PointY="2.9131982421875" /></AssociationConnector>
            </Diagram></edmx:Diagrams>
      </edmx:Designer>
    </edmx:Edmx>
    Fichiers attachés Fichiers attachés

  2. #2
    Rédacteur
    Avatar de Thomas Lebrun
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    9 161
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 9 161
    Par défaut
    Dans le DataType, je vois que tu as du Node et ChildNode: ce sont des classes à toi ?
    Si oui, essaye de mettre {x:Type local:Node} (idem por ChildNode) pour le DataType.

  3. #3
    Membre émérite
    Avatar de FRED.G
    Profil pro
    Inscrit en
    Novembre 2002
    Messages
    1 032
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France

    Informations forums :
    Inscription : Novembre 2002
    Messages : 1 032
    Par défaut
    Salut Thomas

    Non ce ne sont pas des classes à moi. J'ai récupéré très bêtement () ce ce code sur internet et j'avais le vague espoir que ces types (Node & ChildNode) correspondent à des classes de bases servant à construire la représentation de l'arbre xml.

    En fait là où je suis perdu, c'est en matière de syntaxe XPath, et aussi j'ignore quel modèle est exposé par XmlDataProvider (XmlNodeList ??) donc je ne sais pas vraiment si on peut faire des binding "génériques" (par exemple sur des objets XmlNode ou XmlAttribute, etc.) ou s'il faut obligatoirement nommer explicitement les éléments qui composent le xml d'après leur nom métier (dans mon cas : "Schema", "EntityType", etc.).

    Il y a aussi les syntaxes Linq To Xml... Vraiment j'ai du mal à m'y retrouver.

  4. #4
    Membre éclairé
    Avatar de seiryujay
    Profil pro
    Inscrit en
    Mars 2004
    Messages
    950
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2004
    Messages : 950
    Par défaut
    A priori, si tu fais quelque chose comme ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
            <HierarchicalDataTemplate DataType="EntityContainer">
                <TextBlock Text="{Binding XPath=@Name}"/>
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="EntitySet">
                <TextBlock Text="{Binding XPath=@Name}"/>
            </HierarchicalDataTemplate>
            [...]
    Ca devrait marcher.
    Avec dans le champ DataType le type du noeud. Mais par contre, ça t'oblige à détailler le HierarchicalDataTemplate pour chaque type de noeud.
    Je ne sais pas s'il est possible de faire quelque chose de plus générique...

  5. #5
    Membre éclairé
    Avatar de seiryujay
    Profil pro
    Inscrit en
    Mars 2004
    Messages
    950
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2004
    Messages : 950
    Par défaut
    En fait, si c'est possible...
    En gros, ça donne ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
     
            <HierarchicalDataTemplate x:Key="GenericTemplate">
                <TextBlock Text="{Binding XPath=@Name|@Role}" />
            </HierarchicalDataTemplate>
     
            <HierarchicalDataTemplate DataType="EntityContainer"
                                      ItemsSource="{Binding XPath=*}"
                                      ItemTemplate="{StaticResource GenericTemplate}">
                <TextBlock Text="{Binding XPath=@Name}" />
            </HierarchicalDataTemplate>
    Ca va :
    1) regarder tes noeuds de type EntityContainer
    2) les représenter par un TreeViewItem ayant pour Header la valeur contenue dans l'attribut Name de ton noeud
    3) parcourrir tous ses fils
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ItemsSource="{Binding XPath=*}"
    4) pour chacun des fils, se servir de la valeur de l'attribut Name ou Role du noeud comme Header

    J'espère que c'est bien ça que tu voulais...

  6. #6
    Membre émérite
    Avatar de FRED.G
    Profil pro
    Inscrit en
    Novembre 2002
    Messages
    1 032
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France

    Informations forums :
    Inscription : Novembre 2002
    Messages : 1 032
    Par défaut
    Salut,

    merci pour ton aide, j'ai essayé ceci :
    Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
        <Window.Resources>
     
            <HierarchicalDataTemplate x:Key="GenericTemplate">
                <TextBlock Text="{Binding XPath=@Name|@Role}" />
            </HierarchicalDataTemplate>
     
            <HierarchicalDataTemplate DataType="EntityContainer"
                                      ItemsSource="{Binding XPath=*}"
                                      ItemTemplate="{StaticResource GenericTemplate}">
                <TextBlock Text="{Binding XPath=@Name}" />
            </HierarchicalDataTemplate>
     
        </Window.Resources>
     
        <DockPanel>
     
            <DockPanel.DataContext>
                <XmlDataProvider 
                             Source="C:\Users\FRED\Desktop\Northwind.edmx"/>
            </DockPanel.DataContext>
     
     
            <TreeView Name="xmlTreeView" ItemsSource="{Binding XPath=*}">
     
            </TreeView>
               </DockPanel>
    Le Treeview est vide...

    Pour le moment je fais des recherches a niveau de Linq To Xml...

    J'ai quand même l'impression que de toute façon au final, il ne sera pas possible de d'obtenir toute la hiérachie sans la déclarer explicitement en imbriquant des DataTamplate. C'est dommage, avec les regroupements sur une vue c'est possible. Tanpis.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Diviser un fichier XML en plusieurs fichiers XML avec XSLT
    Par bobkorn dans le forum XSL/XSLT/XPATH
    Réponses: 2
    Dernier message: 18/04/2008, 12h13
  2. Decomposé un fichier XML en plusieurs fichiers XML sous python
    Par elhout dans le forum Général Python
    Réponses: 6
    Dernier message: 12/03/2007, 14h20
  3. [XSLT] transfo d'un fichier XML en plusieurs fichiers XML
    Par doudou_rennes dans le forum XSL/XSLT/XPATH
    Réponses: 5
    Dernier message: 28/11/2006, 12h01
  4. [XSLT]transformer un fichier XML en en fichier XML
    Par alexandra dans le forum XSL/XSLT/XPATH
    Réponses: 4
    Dernier message: 16/08/2006, 14h26
  5. Réponses: 4
    Dernier message: 31/07/2006, 09h31

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo