Hassan Said

Hassan Said

  • NA
  • 11
  • 686

how to do databinding between more XML files for TreeView

Dec 22 2016 6:08 AM
Hello,
 
 I have more XML Files and I want to make from this files a Treeview.
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace TreeView_Test
{
    
    public partial class MainWindow : Window
    {
        private TreeViewItem tree; // TreeViewItem reference
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreWhitespace = true;

          
            XmlReader reader = XmlReader.Create("XML1.PKG", settings);
            
           
            tree = new TreeViewItem(); // instantiate TreeViewItem

            tree.Header = "Projekt"; // assign name to TreeViewItem        
            xmlTreeView.Items.Add(tree); // add TreeViewItem to TreeView  
            BuildTree(reader, tree); // build node and tree hierarchy
           
        }

        private void BuildTree(XmlReader reader, TreeViewItem TreeViewItem)
        {
            // TreeViewItem to add to existing tree
            TreeViewItem newNode = new TreeViewItem();

            while (reader.Read())
            {
                // build tree based on node type
                switch (reader.NodeType)
                {
                    // if Text node, add its value to tree
                    case XmlNodeType.Text:
                        newNode.Header = reader.Value;
                        TreeViewItem.Items.Add(newNode);
                        break;
                    case XmlNodeType.EndElement: // if EndElement, move up tree
                        TreeViewItem = (TreeViewItem)TreeViewItem.Parent;
                        break;

                    // if new element, add name and traverse tree
                    case XmlNodeType.Element:

                        // determine if element contains content
                        if (!reader.IsEmptyElement)
                        {
                            // assign node text, add newNode as child
                            newNode.Header = reader.Name;
                            TreeViewItem.Items.Add(newNode);

                            // set TreeViewItem to last child
                            TreeViewItem = newNode;
                        } // end if
                        else // do not traverse empty elements
                        {
                            // assign NodeType string to newNode
                            // and add it to tree
                            newNode.Header = reader.NodeType.ToString();
                            TreeViewItem.Items.Add(newNode);
                        } // end else
                        break;
                    default: // all other types, display node type
                        newNode.Header = reader.NodeType.ToString();
                        TreeViewItem.Items.Add(newNode);
                        break;
                } // end switch

                newNode = new TreeViewItem();
            } // end while

                  
        }

    

    }
       }
 
 ---------------------------------------------------------------------------------------------------------------------------------------------
thanks

Attachment: TreeView_Test.zip

Answers (1)