【发布时间】:2020-02-18 21:37:35
【问题描述】:
我有一个来自需要导出到 xml 的数据库的文件夹路径列表。原始数据如下:
我需要做的是在 xml 中创建一个类似于树的结构:
- networkAdd
- users
- test1
- delete unicode character test
- character test 1
- linked to folder
- character test 2
- character test 3
- sp2013
- newTestsite
- newTestLib
- sampleFolder
- Renamed at source again
- SecurityTest2013Folder
- Shared Documents
- sample.folder
我目前有一个有效的 xml 写入方法可供我使用,但它需要一个树视图。我拿了上面的列表(来自数据库)并将其转换为可以与此方法一起使用的树视图(效果很好),但它需要我先转换为效率低下的树视图。我使用此代码:
public static TreeView PopulateTreeView(IEnumerable<FolderInfo> paths)
{
var treeView = new TreeView();
treeView.PathSeparator = "\\";
TreeNode lastNode = null;
string subPathAgg;
string lastRootFolder = null;
foreach (var item in paths)
{
var path = item.FolderName; // folder path.
if (lastRootFolder != item.FolderRoot)
{
lastRootFolder = item.FolderRoot;
lastNode = null;
}
subPathAgg = string.Empty;
foreach (string subPath in path.Split('\\'))
{
if (subPath.Length > 0)
{
subPathAgg += subPath + "\\";
TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
var newNode = new TreeNode
{
Name = subPathAgg,
Text = subPath,
ImageIndex = 2,
ToolTipText = item.FullFolderPath
};
if (nodes.Length == 0)
{
if (lastNode == null)
treeView.Nodes.Add(newNode);
else
lastNode.Nodes.Add(newNode);
lastNode = newNode;
}
else
lastNode = nodes[0];
}
}
}
return treeView;
}
当我有超过 1000 万条记录要处理时,这行代码的执行变得非常缓慢:
TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
直接从 DB 转换为 XML 对我来说效率更高(没有树视图中间人)。
在考虑嵌套的情况下,有人对将文件夹路径解析为 xml 的替代方法有任何建议吗?提前感谢您的任何指点!
【问题讨论】:
-
如果你可以确保你的输入是有序的,你应该能够通过一些
XStreamingElements 和一堆你当前在目录结构中的位置非常有效地做到这一点 -
在以下帖子中查看我的答案:stackoverflow.com/questions/60281076/…
标签: c# xml directory treeview xmlwriter