Something like this should work:
var doc = XDocument.Load("test.xml");
var folderToFind = textbox_folder_to_find.Text;
var paths = doc.Descendants("dir")
.Where(dir => string.Equals(folderToFind, (string)dir.Attribute("name"), StringComparison.OrdinalIgnoreCase))
.Select(dir => dir.AncestorsAndSelf().Select(el => (string)el.Attribute("name")).Reverse().Aggregate(string.Empty, Path.Combine))
;
foreach (string path in paths)
{
Process.Start(new ProcessStartInfo
{
FileName = path,
UseShellExecute = true,
Verb = "open"
});
}
EDIT: To solve the issues you raised in the comments, you'll need to change how you generate the XML file:
public static XElement GetDirectoryXml(DirectoryInfo root)
{
if (root == null) throw new ArgumentNullException("root");
if (!root.Exists) throw new DirectoryNotFoundException(string.Format("The directory '{0}' does not exist.", root.FullName));
var result = new XElement("dir", new XAttribute("name", root.FullName));
BuildDirectoryXml(root, result);
return result;
}
private static void BuildDirectoryXml(DirectoryInfo currentPath, XElement parentElement)
{
try
{
foreach (var subDirectory in currentPath.EnumerateDirectories())
{
var element = new XElement("dir", new XAttribute("name", subDirectory.Name));
BuildDirectoryXml(subDirectory, element);
parentElement.Add(element);
}
}
catch (UnauthorizedAccessException)
{
}
}