I have a list of folder paths coming from a database that I need to export to xml. The raw data looks as follows:
What I need to do is create a tree like structure, similar to this within the 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
I have current got an efficient xml write method available to me BUT it requires a treeview. I took the list above (coming from the database) and converted it to a treeview that could be used with this method (which works fine) but it requires me to convert to a treeview first which is inefficient. I use this code:
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;
}
This line of code becomes very slow to execute when I have over 10 million records to process:
TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
It would be much more efficient for me to convert straight from DB to XML (without the treeview middle man).
Has anyone any advice on an alternative way of parsing folder paths into xml, taking nesting into consideration? Thanks for any pointers in advance!