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

  • Home
  • SEARCH
  • 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 9203451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:28:36+00:00 2026-06-17T23:28:36+00:00

I have a problem with saving my data from TreeView . I’ve googled a

  • 0

I have a problem with saving my data from TreeView. I’ve googled a lot of topics how to save data from TreeView to XML, and I saw i few methods how to make that. But all of them were using WindowsForms application, and nobody – WPF. I tried to convert those examples to WPF, but no one from them do not work correct. Is there any way to make it? One of the ways I tried to port:

    void SaveToXml(TreeView tw)
    {
        XmlDocument d = new XmlDocument();
        XmlNode n = d.CreateNode(XmlNodeType.Element, "root", "");
        foreach (TreeViewItem t in tw.Items)
        {
            n.AppendChild(getXmlNode(t, d));
        }
        d.AppendChild(n);
        d.Save(AppDomain.CurrentDomain.BaseDirectory + "out.xml");
    }
    private XmlNode getXmlNode(TreeViewItem tnode, XmlDocument d)
    {
        XmlNode n = d.CreateNode(XmlNodeType.Element, tnode.Name, " ");

        foreach (TreeViewItem t in tnode.Items)
        {
            n.AppendChild(getXmlNode(t, d));
        }
        return n;
    }

It throws ArgumentException “Local element’s and attribute’s names cannot be empty”. I’m trying to save info about files on my D:\ drive using code:

  private void ListDirectory(TreeView treeView, string path)
    {
        var rootDirectoryInfo = new DirectoryInfo(path);
        treeView.Items.Add(CreateDirectoryNode(rootDirectoryInfo));
    }

    private static TreeViewItem CreateDirectoryNode(DirectoryInfo directoryInfo)
    {
        var directoryNode = new TreeViewItem();
        directoryNode.Header = directoryInfo.Name;
        try
        {
            foreach (var directory in directoryInfo.GetDirectories())
                directoryNode.Items.Add(CreateDirectoryNode(directory));
            foreach (var file in directoryInfo.GetFiles())
                directoryNode.Items.Add(new TreeViewItem { Header = file.Name });
        }
        catch (UnauthorizedAccessException e)
        { }
        return directoryNode;
    }

It works fine, and TreeView shows all the folders and files. Please, show me how to save data from TreeView to XML. Thanks.

Tried this:

  public void exportToXml(TreeView tv, string filename)
    {
        sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
        //Write the header
        sr.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
        //Write our root node
        sr.WriteLine("<ROOT>");
        foreach (TreeViewItem node in tv.Items)
        {
            saveNode(node.Items);
        }
        //Close the root node
        sr.WriteLine("</ROOT>");
        sr.Close();
    }

    private void saveNode(TreeViewItem[] tnc)
    {
        foreach (TreeViewItem node in tnc)
        {
            //If we have child nodes, we'll write 
            //a parent node, then iterrate through
            //the children
            if (node.Items.Count > 0)
            {
                sr.WriteLine("<" + node.Name + ">");
                saveNode(node.Items);
                sr.WriteLine("</" + node.Name + ">");
            }
            else //No child nodes, so we just write the text
                sr.WriteLine(node.Name);
        }
    }

But saveNode(node.Items); says that there are different types between node.Items and TreeViewItem[], so I tried List<TreeViewItem>, but error is the same.

  • 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-06-17T23:28:37+00:00Added an answer on June 17, 2026 at 11:28 pm

    I’m afraid this is not a real answer, but you may try replacing this line :

    saveNode(node.Items); 
    

    with this one :

    saveNode(node.Items.OfType<TreeViewItem>().ToArray()); 
    

    Hope this will help,


    Example of getting an indented string from an xml doc :

    public static String ToString(XmlDocument doc)
    {
        StringBuilder sb = new StringBuilder();
        XmlTextWriter mywriter = new XmlTextWriter(new StringWriter(sb));
        mywriter.Formatting = Formatting.Indented;
        doc.WriteTo( mywriter );
        mywriter.Flush();
        return(sb.ToString());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with saving data from Ext.data.DirectStore to server Here is my
i'm saving/retrieving data from a plist file but i have a problem, i can't
I have a problem with saving the path in doctrine from a file upload.
I have a problem with my tableview. I am saving all of my data
I have data objects with methods for loading, saving, updating data. Then I have
am having problem in Saving some data in my Java code. I have like
I'm using Core data on my app ok, i have a problem when saving
Hi I have a problem saving 2 tables of my database at the same
I am using CakePHP framework with MySQL database and I have problem in saving
I have problem with UIWebView delay when the load image from url. In my

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.