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 4109444
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:47:51+00:00 2026-05-20T21:47:51+00:00

I have a XML databound to a TreeView with a XmlDataProvider. If i add

  • 0

I have a XML databound to a TreeView with a XmlDataProvider. If i add a subnode to the XML the TreeView shows it, but how can i select this item?

XAML:

<Window.Resources>
    <HierarchicalDataTemplate DataType="category" ItemsSource="{Binding XPath=child::node()}">
        <TextBlock Text="{Binding XPath=@name}" FontWeight="Bold" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="card">
        <TextBlock Text="{Binding XPath=./title}" FontStyle="Italic" />
    </HierarchicalDataTemplate>

    <XmlDataProvider x:Key="dataxml" XPath="root/cards"/>

</Window.Resources>

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

CS:

public partial class MainWindow : Window
{
    XmlDataProvider xmlDataProvider = new XmlDataProvider();

    public MainWindow()
    {
        InitializeComponent();           
        xmlDataProvider = this.FindResource("dataxml") as XmlDataProvider;
        xmlDataProvider.Source = new Uri(System.IO.Path.GetFullPath(fullPathToXml), UriKind.Absolute);
        xmlDataProvider.Refresh();
    }

    public void AddChild()
    {
        XmlNode newNode = xmlDataProvider.Document.CreateElement("card");
        XmlNode selectedItem = (XmlNode)treeView.SelectedItem;

        if (selectedItem != null)
        {
            //add the newNode as child to the selected
            selectedItem.AppendChild(newNode);
            //select the childnode (newNode) ?????       <=====
        }
        else
        {
            //add the newNode as child to the rootnode and select it:
            xmlDataProvider.Document.DocumentElement["cards"].AppendChild(newNode);
            (treeView.ItemContainerGenerator.ContainerFromItem(newNode) as TreeViewItem).IsSelected = true;
        }

        xmlDataProvider.Document.Save(fullPathToXml);
        xmlDataProvider.Refresh();
    }
}

XML:

<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-20T21:47:52+00:00Added an answer on May 20, 2026 at 9:47 pm

    It is late, so my functions arent the smoothest, but it works!
    I have comment it for all that once have the same problem 😉

    Usings:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Controls;
    using System.Xml;
    using System.Runtime.InteropServices;
    

    Function:

    /// <summary>
    /// Select a XmlNode in a TreeView that is databound to a xmlDataProvider.
    /// </summary>
    /// <param name="treeView">A referenz to the TreeView.</param>
    /// <param name="node">The node to select.</param>
    public static void SelectTreeViewNode(ref TreeView treeView, XmlNode node)
    {
        if (treeView.HasItems)
        {
            //cast to xml-nodes
            var xmlNodeList = treeView.Items.Cast<XmlNode>(); ;
    
            //node at root level? -> select it
            if (xmlNodeList.Contains(node))
            {
                (treeView.ItemContainerGenerator.ContainerFromItem(node) as TreeViewItem).IsSelected = true;
            }
            else
            {
                //get rootnode
                XmlNode rootNode =  GetRootNode(node, xmlNodeList);
    
                //get a list of parent nodes
                List<XmlNode> parentNodes = new List<XmlNode>();
                GetAllParentNodes(rootNode, node, ref parentNodes);
                parentNodes.Reverse();
    
                //finaly, select the node
                SelectNode(parentNodes, node, ref treeView, null);  
            }
    
        }
    }
    
    /// <summary>
    /// Goes recursiv down the parent nodes until he finds a node that is in the xmlNodeList.
    /// Returns null if he can´t find anything.
    /// </summary>
    /// <param name="node">The start node.</param>
    /// <param name="xmlNodeList">A list with possible rootnodes.</param>
    /// <returns>The rootnode</returns>
    private static XmlNode GetRootNode(XmlNode node, IEnumerable<XmlNode> xmlNodeList)
    {
        if (!xmlNodeList.Contains(node) && node.ParentNode != null)
        {
            return GetRootNode(node.ParentNode, xmlNodeList);
        }
        else if (xmlNodeList.Contains(node)) return node;
        else return null;
    }
    
    /// <summary>
    /// Returns all parent nodes from the actual node within the rootNode. Works recursiv.
    /// </summary>
    /// <param name="rootNode">The rootnode.</param>
    /// <param name="actualNode">The startnode</param>
    /// <param name="parentNodes">The rererenz to the outputlist</param>
    private static void GetAllParentNodes(XmlNode rootNode, XmlNode actualNode, ref List<XmlNode> parentNodes)
    {
        if (actualNode.ParentNode != null && !actualNode.Equals(rootNode))
        {
            parentNodes.Add(actualNode.ParentNode);
            GetAllParentNodes(rootNode, actualNode.ParentNode, ref parentNodes);
        }
    }
    
    /// <summary>
    /// Select a XmlNode from a TreeView that is databound to a xmlDataProvider.
    /// </summary>
    /// <param name="parentNodes">All the parent nodes, first in the list is the rootnode.</param>
    /// <param name="node">The node to select.</param>
    /// <param name="treeView">A referenz to the TreeView.</param>
    /// <param name="item">Variable for the recursion.</param>
    private static void SelectNode(List<XmlNode> parentNodes, XmlNode node, ref TreeView treeView, [Optional, DefaultParameterValue(null)] TreeViewItem item)
    {
        if (parentNodes.Count > 0)
        {
            TreeViewItem tvItem;
            if (item != null)
                tvItem = item.ItemContainerGenerator.ContainerFromItem(parentNodes.First()) as TreeViewItem;
            else 
                tvItem = treeView.ItemContainerGenerator.ContainerFromItem(parentNodes.First()) as TreeViewItem;
            parentNodes.RemoveAt(0);
            SelectNode(parentNodes, node, ref treeView, tvItem);
        }
        else if (item != null)
        {
            (item.ItemContainerGenerator.ContainerFromItem(node) as TreeViewItem).IsSelected = true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two TreeView´s in a TabControl, databound to a xmlDataProvider. If i add
Can someone please help me, I have this xml snippet <?xml version=1.0 encoding=utf-8 ?>
I have this xml file, and now i will want to add new entries
I have an XML that needs to be databound to a WPF TreeView .
I have a GridView which is databound to an XML Datasource. For one of
I have XML that looks like this: <Parameter Name=parameter name Value=parameter value /> which
I have xml like this: <configurationData> <path name='b'> <path name='a'> <setting name='s1'> ![CDATA[XXXX]] </setting>
I have XML that looks like this: <ROW ref=0005631 type=04 line=1 value=Australia/> <ROW ref=0005631
I have XML like, <items> <item> <products> <product>laptop</product> <product>charger</product> <product>Cam</product> </products> </item> <item> <products>
I have xml structure like this: <Group id=2 name=Third parentid=0 /> <Group id=6 name=Five

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.