【问题标题】:XDocument Load Multiple XML Files From Multiple Folders At OnceXDocument 一次从多个文件夹加载多个 XML 文件
【发布时间】:2014-09-05 10:52:00
【问题描述】:

如何使用 XDocument.Load(paths) 一次从多个文件夹加载多个 XML 文件?

我想要这样做,以便我可以在网页中显示每个 XML 文件。

文件结构类似于 XMLFiles --> Years --> Months --> files.XML。我希望它们一次全部加载。

我正在使用带有 MVC 4 的 Visual Studio 2013。

【问题讨论】:

标签: c# xml visual-studio-2013 linq-to-xml


【解决方案1】:

我认为这没有 @har07 那么多的代码,但它是一样的:

string mainPath = "path where you have all xml"
string[] paths = Directory.GetFiles(mainPath, "search pattern as you need", SearchOption.AllDirectories);

foreach(var path in paths){
XDocument xDoc = XDocument.Load(path);
//something you want to do with xml
}

我认为你不会找到其他解决方案。

【讨论】:

    【解决方案2】:

    您可以像这样修改您在评论中链接的问题的答案:

    string xmlFiles = "path_to_XMLFiles_folder/XMLFiles";
                              //get all folders within XMLFiles folder. the years
    string[] files = Directory.GetDirectories(xmlFiles)
                              //get all folders within each year. the months
                              .SelectMany(Directory.GetDirectories)
                              //get all files within each montsh.
                              .SelectMany(Directory.GetFiles)
                              .Where(f => f.EndsWith(".xml"))
                              .ToArray();
    foreach(var path in files)
    {  
        XDocument xDoc = XDocument.Load(path);
        //process each XML file
    }
    

    我不确定这是否适合您的最终目标,但无论如何,这就是您在评论中提出的要求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-27
      • 2011-04-15
      • 2010-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      相关资源
      最近更新 更多