我如何在Xml文件中搜索文件夹的路径 [英] How I Search In Xml File For A Path Of A Giving Folder

查看:107
本文介绍了我如何在Xml文件中搜索文件夹的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定用另一个程序来测试创建一个包含目录的所有文件夹条带的XML文件。运行良好,创建文件是正确的。这是代码:



I decided to do another program to test the creation of A XML file with all folder strocture of a directory. Is working well, the file is created think correctly. Here's the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Xml;
using System.Xml.Linq;

namespace generate_XML_open_folder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button_generate_XML_Click(object sender, EventArgs e)
        {
            string rootPath = combobox_rootpath.Text;
            var dir = new DirectoryInfo(rootPath);

            var doc = new XDocument(GetDirectoryXml(dir));

            doc.Save("test.xml");
        }

        private void button_open_folder_Click(object sender, EventArgs e)
        {

        }

        private void textbox_folder_to_find_TextChanged(object sender, EventArgs e)
        {

        }

        private void combobox_rootpath_SelectedIndexChanged(object sender, EventArgs e)
        {
            combobox_rootpath.Items.Clear();
            foreach (string s in Directory.GetLogicalDrives())
            {

                combobox_rootpath.Items.Add(s);


            }
        }
        public static XElement GetDirectoryXml(DirectoryInfo dir)
        {
            var info = new XElement("dir",
                           new XAttribute("name", dir.Name));

            //foreach (var file in dir.GetFiles())
            // info.Add(new XElement("file",
            //  new XAttribute("name", file.Name)));

            foreach (var subDir in dir.GetDirectories())
                info.Add(GetDirectoryXml(subDir));

            return info;
        }

    }
}





这是一个示例,内容为XML文件:





Here is an example with the content of the XML file:

<?xml version="1.0" encoding="utf-8"?>
<dir name="h:\">
  <dir name="System Volume Information" />
  <dir name="xp1000" />
  <dir name="Procura_Desenhos">
    <dir name="Procura_Desenhos">
      <dir name="Resources" />
      <dir name="Properties" />
      <dir name="obj">
        <dir name="Release" />
        <dir name="Debug">
          <dir name="TempPE" />
          <dir name="Refactor" />
        </dir>
      </dir>
      <dir name="bin">
        <dir name="Release" />
        <dir name="Debug" />
      </dir>
    </dir>
  </dir>
  <dir name="Procura_Desenhos1.1">
    <dir name="Procura_Desenhos1.1">
      <dir name="bin">
        <dir name="Debug" />
        <dir name="Release" />
      </dir>
      <dir name="obj">
        <dir name="Debug">
          <dir name="TempPE" />
        </dir>
      </dir>
      <dir name="Properties" />
      <dir name="Resources" />
    </dir>
  </dir>
  <dir name="Backup">
    <dir name="Visual 2008">
      <dir name="Projects">
        <dir name="Procura_Desenhos">
          <dir name="Procura_Desenhos">
            <dir name="Resources" />
            <dir name="Properties" />
            <dir name="obj">
              <dir name="Release" />
              <dir name="Debug">
                <dir name="TempPE" />
                <dir name="Refactor" />
              </dir>
            </dir>
            <dir name="bin">
              <dir name="Release" />
              <dir name="Debug" />
            </dir>
          </dir>
        </dir>
      </dir>
    </dir>
    <dir name="Visual 2005">
      <dir name="Projects">
        <dir name="WindowsApplication1">
          <dir name="WindowsApplication1">
            <dir name="obj">
              <dir name="Release" />
              <dir name="Debug">
                <dir name="TempPE" />
              </dir>
            </dir>
            <dir name="My Project" />
            <dir name="bin">
              <dir name="Release" />
              <dir name="Debug" />
            </dir>
          </dir>
        </dir>
        <dir name="Procura">
          <dir name="Procura">
            <dir name="Resources" />
            <dir name="obj">
              <dir name="Release" />
              <dir name="Debug">
                <dir name="TempPE" />
              </dir>
            </dir>
            <dir name="My Project" />
            <dir name="bin">
              <dir name="Release" />
              <dir name="Debug" />
            </dir>
          </dir>
        </dir>
        <dir name="find_v1.1">
          <dir name="_UpgradeReport_Files" />
          <dir name="find_v1.1">
            <dir name="Properties" />
            <dir name="obj">
              <dir name="Release" />
              <dir name="Debug">
                <dir name="TempPE" />
              </dir>
            </dir>
            <dir name="bin">
              <dir name="Release" />
              <dir name="Debug" />
            </dir>
          </dir>
          <dir name="Backup">
            <dir name="find_v1.1">
              <dir name="Properties" />
            </dir>
          </dir>
        </dir>
        <dir name="find_v1.0">
          <dir name="find_v1.0">
            <dir name="Properties" />
            <dir name="obj">
              <dir name="Release" />
              <dir name="Debug">
                <dir name="TempPE" />
              </dir>
            </dir>
            <dir name="bin">
              <dir name="Release" />
              <dir name="Debug" />
            </dir>
          </dir>
        </dir>
      </dir>
    </dir>
  </dir>
</dir>





Now what I wan t is to enter in textbox_folder_to_find the name of the folder to look in , the program searchs in the XML file for the path of that folder and after having the path is opened with shellExecute or explorer. Does anyone know how I can do this?



Now what I want is to enter in textbox_folder_to_find the name of the folder to look in , the program searchs in the XML file for the path of that folder and after having the path is opened with shellExecute or explorer. Does anyone know how I can do this?

推荐答案

Something like this should work:

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






To solve the issues you raised in the comments, you’ll need to change how you generate the XML file:




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


这篇关于我如何在Xml文件中搜索文件夹的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆