从TreeView保存到XML

时间:2013-01-29 08:21:42

标签: c# wpf xml silverlight treeview

我从TreeView保存数据时遇到问题。我已经搜索了很多主题如何将数据从TreeView保存到XML,我看到了一些方法如何制作。但他们所有人都在使用WindowsForms application,而没有人 - WPF。我试图将这些示例转换为WPF,但是没有人能够正常工作。有什么方法可以做到吗?我试图移植的方法之一:

    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;
    }

抛出ArgumentException“本地元素和属性的名称不能为空”。我正在尝试使用代码保存有关D:\驱动器上文件的信息:

  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;
    }

工作正常,TreeView显示所有文件夹和文件。请告诉我如何将数据从TreeView保存到XML。感谢。

试过这个:

  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);
        }
    }

但是saveNode(node.Items);node.ItemsTreeViewItem[]之间有不同的类型,所以我尝试了List<TreeViewItem>,但错误是一样的。

1 个答案:

答案 0 :(得分:0)

我担心这不是一个真正的答案,但你可以尝试更换这一行:

saveNode(node.Items); 

这一个:

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

希望这会有所帮助,


从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());
}