0

我在保存我的数据时遇到问题TreeView。我用谷歌搜索了很多关于如何将数据保存到的主题TreeViewXML我看到了一些方法来做到这一点。但是他们都在使用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显示所有文件夹和文件。请告诉我如何将数据保存TreeViewXML. 谢谢。

试过这个:

  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.Itemsand之间有不同的类型TreeViewItem[],所以我尝试List<TreeViewItem>了,但是错误是一样的。

4

1 回答 1

0

恐怕这不是一个真正的答案,但您可以尝试替换此行:

saveNode(node.Items); 

有了这个:

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

希望这会有所帮助,


从 xml 文档中获取缩进字符串的示例:

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());
}
于 2013-01-29T08:49:26.067 回答