Reputation: 93
How do I load multiple XML files from multiple folders at once using XDocument.Load(paths)?
I am wanting to to this so that I can display each XML file in a webpage.
The file structure is like XMLFiles --> Years --> Months --> files.XML. I want them all loading at once.
I am using Visual Studio 2013 with MVC 4.
Upvotes: 0
Views: 1677
Reputation: 1081
I think this has not as many code as @har07 but it is the same:
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
}
I think you will not find another solution.
Upvotes: 1
Reputation: 89325
You can modify the answer of the question you linked in your comment like so :
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
}
I'm not sure this will suits your ultimate goal, but anyway, this is what you asked in the comment.
Upvotes: 0