126

I want to read all xml files inside a particular folder in c# .net

XDocument doc2 = XDocument.Load((PG.SMNR.XMLDataSourceUtil.GetXMLFilePath(Locale, "Products/category/product.xml")));

i have multiple products in category folder.. want loop the folder and should get all product xml file names.

XDocument doc2 = XDocument.Load((PG.SMNR.XMLDataSourceUtil.GetXMLFilePath(Locale, "Products/category/x1.xml")));
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
ram
  • 1,279
  • 2
  • 8
  • 7
  • 5
    What have you tried? What didn't work? Where are you having difficulties? http://tinyurl.com/so-hints – Oded Apr 30 '11 at 07:56

7 Answers7

295
using System.IO;
...
foreach (string file in Directory.EnumerateFiles(folderPath, "*.xml"))
{
    string contents = File.ReadAllText(file);
}

Note the above uses a .NET 4.0 feature; in previous versions replace EnumerateFiles with GetFiles). Also, replace File.ReadAllText with your preferred way of reading xml files - perhaps XDocument, XmlDocument or an XmlReader.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    'System.IO.Directory' does not contain a definition for 'EnumerateFiles' – ram Apr 30 '11 at 08:11
  • 8
    "using System.IO;" Brilliant!! Why can more people not give this? It can be so annoying to attempt to find what people are talking about – user001 Nov 30 '15 at 11:03
  • 1
    @user001 because usually the IDE can find and add such things for you - just press ctrl+. – Marc Gravell Nov 30 '15 at 15:58
  • 1
    @MarcGravell haha usually :( – user001 Nov 30 '15 at 16:09
  • I think it is better to use StringBuilder inside the loop to append every file's content. sb.Append(File.ReadAllText(file)); – e0x3 Feb 20 '17 at 05:51
  • @Vitrum nowhere did it say anything about appending them. Whatever the code does with the contents of each file: fine. But it doesn't mention appending *anywhere*. If you are appending, `ReadAllText` is also overhead - should loop inside the file, chunking. – Marc Gravell Feb 20 '17 at 07:39
  • Please put some example for folder path demo @Marc Gravell – Ibrahim Inam Feb 13 '19 at 07:46
34
using System.IO;

DirectoryInfo di = new DirectoryInfo(folder);
FileInfo[] files = di.GetFiles("*.xml");
Adi
  • 5,113
  • 6
  • 46
  • 59
16
using System.IO;

//...

  string[] files;

  if (Directory.Exists(Path)) {
    files = Directory.GetFiles(Path, @"*.xml", SearchOption.TopDirectoryOnly);
    //...
Matthias Alleweldt
  • 2,453
  • 17
  • 16
8

You can use the DirectoryInfo.GetFiles method:

FileInfo[] files = DirectoryInfo.GetFiles("*.xml");
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
trydis
  • 3,905
  • 1
  • 26
  • 31
6

Try this It is working for me..

The syntax is Directory.GetFiles(string path, string searchPattern);

var filePath = Server.MapPath("~/App_Data/");
string[] filePaths = Directory.GetFiles(@filePath, "*.*");

This code will return all the files inside App_Data folder.

The second parameter . indicates the searchPattern with File Extension where the first * is for file name and second is for format of the file or File Extension like (*.png - any file name with .png format.

Chandan Kumar
  • 221
  • 11
  • 14
6

If you are looking to copy all the text files in one folder to merge and copy to another folder, you can do this to achieve that:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace HowToCopyTextFiles
{
  class Program
  {
    static void Main(string[] args)
    {
      string mydocpath=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);     
      StringBuilder sb = new StringBuilder();
      foreach (string txtName in Directory.GetFiles(@"D:\Links","*.txt"))
      {
        using (StreamReader sr = new StreamReader(txtName))
        {
          sb.AppendLine(txtName.ToString());
          sb.AppendLine("= = = = = =");
          sb.Append(sr.ReadToEnd());
          sb.AppendLine();
          sb.AppendLine();   
        }
      }
      using (StreamWriter outfile=new StreamWriter(mydocpath + @"\AllTxtFiles.txt"))
      {    
        outfile.Write(sb.ToString());
      }   
    }
  }
}
Lynn Crumbling
  • 12,985
  • 8
  • 57
  • 95
gaurav balyan
  • 61
  • 1
  • 1
2
    using System.IO;
    string[] arr=Directory.GetFiles("folderpath","*.Fileextension");
      foreach(string file in arr)
       {

       }
Rahul sahu
  • 31
  • 1
  • 1
    Whilst this solution may answer the OP's problem, it is advised not to just write a code-only answer as this may not be very helpful to future users. Elaborate a little. What does this solution provide? How is it going to benefit the OP? etc. – Geoff James May 11 '17 at 11:46
  • i just seen yesterday code but that that is not a copy paste – Rahul sahu May 11 '17 at 11:48
  • I wasn't implying that you'd copy/pasted. I've updated my comment. Still, my comment still stands. Elaborate. – Geoff James May 11 '17 at 11:50