Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to C# and VS and am trying to convert XmlNode to TreeViewItem and reflect the changes into TreeView in a recursive manner but TreeView is not updated/ not showing the nodes. Below is my TreeView.xaml code:

C#
<TreeView x:Name="DAPITreeView"  Margin="5,5,5,-596" Grid.RowSpan="11" Height="829" SelectedItemChanged="DAPITreeView_SelectedItemChanged" >
     <TreeView.ItemContainerStyle>
         <Style TargetType="{x:Type TreeViewItem}">
             <Setter Property="IsExpanded" Value="True"/>
         </Style>
     </TreeView.ItemContainerStyle>
 </TreeView>

Below is my TreeView.xaml.cs:
C#
private void LoadTreeView(string filename)
{
    XmlDocument doc = new XmlDocument();
    try
    {
        doc.Load(filename + ".xml");
    }
    catch (Exception Err)
    {
        System.Windows.Forms.MessageBox.Show(Err.Message);
        return;
    }

    ConvertXmlNodeToTreeNode(doc, DAPITreeView.Items);
    //Tree View Expand All to be Done
}

private void ConvertXmlNodeToTreeNode(XmlNode xmlNode, ItemCollection itemCollection)
{
    TreeViewItem newTreeViewItem = null;

    switch (xmlNode.NodeType)
    {
        case XmlNodeType.ProcessingInstruction:
        case XmlNodeType.XmlDeclaration:
            break;
        case XmlNodeType.Element:
            {
                newTreeViewItem = new TreeViewItem();
                newTreeViewItem.Items.Add(xmlNode.Name);
                itemCollection.Add(newTreeViewItem);    //TreeView not updated here
            }
            break;
        case XmlNodeType.Comment:
            {
                newTreeViewItem = new TreeViewItem();
                newTreeViewItem.Items.Add(xmlNode.Name);
                newTreeViewItem = new TreeViewItem();
                newTreeViewItem.Items.Add("<!--" + xmlNode.Value + "-->");
            }
            break;
    }

    if (xmlNode.Name == "message")
    {
        if (xmlNode.ChildNodes.Count > 0)
        {
            newTreeViewItem = new TreeViewItem();
            newTreeViewItem.Items.Add("dummy");
        }
        return ;
    }
    foreach (XmlNode childNode in xmlNode.ChildNodes)
    {
        if (newTreeViewItem == null)
        {
            newTreeViewItem = new TreeViewItem();
            itemCollection.Add(xmlNode.Name);        //Only item added to TreeView as #document.
            newTreeViewItem.Items.Add(xmlNode.Name);
        }
        ConvertXmlNodeToTreeNode(childNode, newTreeViewItem.Items);
    }
}


Can anyone suggest me what is missing or needs correction?


What I have tried:

I have referred many blogs but not the right solution for the above problem.
Posted
Updated 28-Oct-17 23:15pm
Comments
Graeme_Grant 27-Oct-17 3:38am    
"TreeView is not updated/ not showing the nodes"

Have you learned first how to work with the TreeView control in a mock/prototype BEFORE trying to use it? Do you understand how to use the Debugger to check that data is set correctly?

If you use a data-first model, not UI-first as per your code above, the TreeView control is much easier to work with. Have a look at this Code Project article: Simplifying the WPF TreeView by Using the ViewModel Pattern[^]

1 solution

Found the correction. Thanks for the comments.
C#
newTreeViewItem.Header = ("dummy");
//newTreeViewItem.Items.Add("dummy");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900