Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4038512
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:28:55+00:00 2026-05-20T12:28:55+00:00

i have the following xml structure and want to add it to a treeview.

  • 0

i have the following xml structure and want to add it to a treeview. below is my first test.
my problem is that i don´t know how to bind the list to the treeview because the childelements must consists of poth elements (category & card).

-cards
  -category
    -card
    -card
    -category
      -card
      -card
      -card

<Window.Resources>
    <HierarchicalDataTemplate DataType="cards" ItemsSource="{Binding XPath=card}">
        <TextBlock Text="My Cards" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="category" ItemsSource="{Binding XPath=card}">
        <TextBlock Text="{Binding XPath=@name}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="card" ItemsSource="{Binding XPath=category}">
        <TextBlock Text="{Binding XPath=@name}"/>
    </HierarchicalDataTemplate>

    <XmlDataProvider x:Key="dataxml" XPath="cards" Source="folder\cards.xml" />
</Window.Resources>

<TreeView Name="treeView" ItemsSource="{Binding Source={StaticResource dataxml}, XPath=.}" />

Sample of the XML:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <settings>
    ....
    ..
  </settings>
  <cards>
    <category name="C1">
        <card name="card1">
            <question>bla</question>
            <answer>blub</answer>
        </card>
        <category name="C2">
            <card name="card4">
                <question>bla</question>
                <answer>blub</answer>
            </card>
        </category>
    </category>
        <card name="card2">
            <question>bla</question>
            <answer>blub</answer>
        </card>
        <card name="card3">
            <question>bla</question>
            <answer>blub</answer>
        </card>
  </cards>
</root>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:28:56+00:00Added an answer on May 20, 2026 at 12:28 pm

    A generic approach can be done like this; which IMHO is a nice approach for the data if it is indeed arbitrary.

           <HierarchicalDataTemplate x:Key="NodeTemplate">
                <TextBlock x:Name="tb"/>
                <HierarchicalDataTemplate.ItemsSource>
                    <Binding XPath="child::node()" />
                </HierarchicalDataTemplate.ItemsSource>
                <HierarchicalDataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
                        <Setter TargetName="tb" Property="Text" Value="{Binding Path=Value}"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                        <Setter TargetName="tb" Property="Text" Value="{Binding Path=Name}"></Setter>
                    </DataTrigger>
                </HierarchicalDataTemplate.Triggers>
           </HierarchicalDataTemplate>
    
           <XmlDataProvider x:Key="dataxml" XPath="root" Source="cards.xml"/>
    

    The TreeView would then look like this…

    <TreeView HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="treeView"
                      ItemTemplate="{StaticResource NodeTemplate}"
                      ItemsSource="{Binding Source={StaticResource dataxml}, XPath=.}" />
    

    If the data is not arbitrary I would suggest parsing the XML via LINQ or other method and moving the data into a composite type which would could then be bound to via the HierarchicalDataTemplate in a more granular fashion. If you were to go that route a great walk through should suffice in achieving what you are after.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML structure so that I can add my configuration details
I want have the following output: <?xml version=1.0 encoding=UTF-8?> <structure:structuralDataRoot xmlns:register=http://www.test.ch/register/1 xmlns:structure=http://test.ch/structure/1 > <structure:tester>ZH</structure:tester>
I have an xml file with the following structure: <main_tag> <first> <tag1>val1</tag1> <conf> <tag2>val2</tag2>
one more problem. I have following structure of XML: <player> <id>12</id> <name>pl_name</name> <experience> <points>147</points>
I have an XML document that contains the following structure: Its more or less
I have following XML generated by serializing a boost::posix_time::ptime structure. I want to create
one more problem. I have following structure of XML: <player> <id>12</id> <name>pl_name</name> <experience> <points>147</points>
I have got following XML structure of my screen. I want my Relative Layout
I have the following XML structure: <agencies> <city> <name>New York</name> <address>Street A, 101</address> <phone>111-222-333</phone>
I have the following XML structure: <Main> <Node1>Definite</Node1> <Node2>Definite</Node2> <Node3>Definite</Node3> <Node4>Definite</Node4> <Node5>Definite</Node5> <Node6>Definite</Node6> <A>Possible</A>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.