Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Binding to an XML list of attributes

Binding to an XML list of attributes

Scheduled Pinned Locked Moved WPF
wpfxmlwcfquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jayme65
    wrote on last edited by
    #1

    I have an XML file structured this way:

    <Emulators>
    <System>
    <Name>Name1</Name>
    <Kind name="lexplode"/>
    <Kind name="sexplode"/>
    <Kind name="dropbomb"/>
    </System>
    </Emulators>

    If I want to read, in XAML, from <Name> I know that I have to do:

    <TextBox Text="{Binding XPath=Name, UpdateSourceTrigger=PropertyChanged}"/>

    But what if I want to read all the values under <Kind>> ? And which control could receive them, a textbox, a listbox, ..? Thanks a lot!!

    P 1 Reply Last reply
    0
    • J Jayme65

      I have an XML file structured this way:

      <Emulators>
      <System>
      <Name>Name1</Name>
      <Kind name="lexplode"/>
      <Kind name="sexplode"/>
      <Kind name="dropbomb"/>
      </System>
      </Emulators>

      If I want to read, in XAML, from <Name> I know that I have to do:

      <TextBox Text="{Binding XPath=Name, UpdateSourceTrigger=PropertyChanged}"/>

      But what if I want to read all the values under <Kind>> ? And which control could receive them, a textbox, a listbox, ..? Thanks a lot!!

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      In order to get the attribute, you have to use a slightly different type of syntax. Rather than using XPath=name, you have to use XPath=@name. Now, that will only get you the value of the currently selected Kind item, so how do you actually get that item? Well, the answer is to use something like a ListBox to encapsulate the items.

      <ListBox ItemsSource="{Binding Source={MyEmulatorsList, XPath=//Kind}" ItemTemplate="{StaticResource KindItemsTemplate}" />

      Note: In this sample, I'm assuming that you have used an XmlDataProvider called MyEmulatorsList to point to this XML. So, where is the actual display of the name item? Well, that's provided by the ItemTemplate, which looks like this:

      <DataTemplate x:Key="KindItemsTemplate">
      <TextBlock Text="{Binding XPath=@name}" />
      </DataTemplate>

      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      J 1 Reply Last reply
      0
      • P Pete OHanlon

        In order to get the attribute, you have to use a slightly different type of syntax. Rather than using XPath=name, you have to use XPath=@name. Now, that will only get you the value of the currently selected Kind item, so how do you actually get that item? Well, the answer is to use something like a ListBox to encapsulate the items.

        <ListBox ItemsSource="{Binding Source={MyEmulatorsList, XPath=//Kind}" ItemTemplate="{StaticResource KindItemsTemplate}" />

        Note: In this sample, I'm assuming that you have used an XmlDataProvider called MyEmulatorsList to point to this XML. So, where is the actual display of the name item? Well, that's provided by the ItemTemplate, which looks like this:

        <DataTemplate x:Key="KindItemsTemplate">
        <TextBlock Text="{Binding XPath=@name}" />
        </DataTemplate>

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        J Offline
        J Offline
        Jayme65
        wrote on last edited by
        #3

        Thanks for your reply!! I think that I'd better expose the whole code. XML file:

            Name1
        

        </Emulators>

        VB code:

        Class MainWindow
        Dim data As XmlDataProvider = New XmlDataProvider()
        Private Sub MainWindow_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
        data.Source = New Uri(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\myapplication\Systems.xml")
        data.XPath = "Emulators/System"
        Grid1.DataContext = data
        End Sub
        End Class

        XAML code:

        It doesn't work, I get a

        System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='//Kind' BindingExpression:Path=; DataItem='String' (HashCode=2037252866); target element is 'ListBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') data

        P D 3 Replies Last reply
        0
        • J Jayme65

          Thanks for your reply!! I think that I'd better expose the whole code. XML file:

              Name1
          

          </Emulators>

          VB code:

          Class MainWindow
          Dim data As XmlDataProvider = New XmlDataProvider()
          Private Sub MainWindow_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
          data.Source = New Uri(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\myapplication\Systems.xml")
          data.XPath = "Emulators/System"
          Grid1.DataContext = data
          End Sub
          End Class

          XAML code:

          It doesn't work, I get a

          System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='//Kind' BindingExpression:Path=; DataItem='String' (HashCode=2037252866); target element is 'ListBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') data

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          You are trying to bind to Emulators in your XPath, but your XML says Base.

          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          J 1 Reply Last reply
          0
          • P Pete OHanlon

            You are trying to bind to Emulators in your XPath, but your XML says Base.

            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            J Offline
            J Offline
            Jayme65
            wrote on last edited by
            #5

            You are trying to bind to Emulators in your XPath, but your XML says Base.

            That's a typing mistake in my post (I've corrected it in my post)...but actually the XML file is correct..so the mistake doesn't come from here.

            1 Reply Last reply
            0
            • J Jayme65

              Thanks for your reply!! I think that I'd better expose the whole code. XML file:

                  Name1
              

              </Emulators>

              VB code:

              Class MainWindow
              Dim data As XmlDataProvider = New XmlDataProvider()
              Private Sub MainWindow_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
              data.Source = New Uri(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\myapplication\Systems.xml")
              data.XPath = "Emulators/System"
              Grid1.DataContext = data
              End Sub
              End Class

              XAML code:

              It doesn't work, I get a

              System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='//Kind' BindingExpression:Path=; DataItem='String' (HashCode=2037252866); target element is 'ListBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') data

              D Offline
              D Offline
              dbaseman
              wrote on last edited by
              #6

              I think I see what the problem is. In the ListBox XAML, you don't need the Source=data (that's incorrect syntax, and besides, you already set the DataContext from the code-behind, so it's not needed. Take that part out, and it should work:

              J 1 Reply Last reply
              0
              • D dbaseman

                I think I see what the problem is. In the ListBox XAML, you don't need the Source=data (that's incorrect syntax, and besides, you already set the DataContext from the code-behind, so it's not needed. Take that part out, and it should work:

                J Offline
                J Offline
                Jayme65
                wrote on last edited by
                #7

                Thank you very much!! For the first time I can see those data displayed!! ;-) Just one think... of importance: ALL the "Kind" attributes are displayed. I've tried a "IsSynchronizedWithCurrentItem="True"" but it doesn't change anything. Any idea on how to fix it, please? The datatemplate for the combobox that displays the system's "Name":

                The datatemplate for the ListBox that display the "Kind" name attributes (related to the selected "System"!!):

                The WPF controls:

                1 Reply Last reply
                0
                • J Jayme65

                  Thanks for your reply!! I think that I'd better expose the whole code. XML file:

                      Name1
                  

                  </Emulators>

                  VB code:

                  Class MainWindow
                  Dim data As XmlDataProvider = New XmlDataProvider()
                  Private Sub MainWindow_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
                  data.Source = New Uri(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\myapplication\Systems.xml")
                  data.XPath = "Emulators/System"
                  Grid1.DataContext = data
                  End Sub
                  End Class

                  XAML code:

                  It doesn't work, I get a

                  System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='//Kind' BindingExpression:Path=; DataItem='String' (HashCode=2037252866); target element is 'ListBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') data

                  D Offline
                  D Offline
                  dbaseman
                  wrote on last edited by
                  #8

                  You can select an item by using the XPath expression. Instead of XPath=//Kind, use XPath=//Kind[0], or XPath=//Kind[1], etc.

                  J 1 Reply Last reply
                  0
                  • D dbaseman

                    You can select an item by using the XPath expression. Instead of XPath=//Kind, use XPath=//Kind[0], or XPath=//Kind[1], etc.

                    J Offline
                    J Offline
                    Jayme65
                    wrote on last edited by
                    #9

                    dbaseman, Thanks again for taking time to help me! If you consider my XML file

                        Name1
                        
                        
                        
                    
                    Name2
                    

                    Using "XPath=//Kind[1]" will return: "a1, b1" What I would like is "a1, a2, a3" being returned!! How should I please proceed to achieve this (returning all "Kind" attributes of the currently selected "System"? Thanks very much,

                    D 1 Reply Last reply
                    0
                    • J Jayme65

                      dbaseman, Thanks again for taking time to help me! If you consider my XML file

                          Name1
                          
                          
                          
                      
                      Name2
                      

                      Using "XPath=//Kind[1]" will return: "a1, b1" What I would like is "a1, a2, a3" being returned!! How should I please proceed to achieve this (returning all "Kind" attributes of the currently selected "System"? Thanks very much,

                      D Offline
                      D Offline
                      dbaseman
                      wrote on last edited by
                      #10

                      I'm not really an XPath expert, but my guess would be: //System[1]/Kind

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups