2014-06-17 126 views
-3

我必須從物理位置讀取任何xml文件。 我正在做下面的方式,但是,它現在說文件現在找到了。從物理位置讀取任何xml

例如,我可以有任何文件(a.xml,b.xml,c.xml,..... z.xml)
所以我想用一個通用的代碼來讀取xml。

任何幫助? 謝謝

+0

如果存在多個匹配* .xml文件的文件,您需要哪種文件內容?他們全部? – dummy

回答

2

Load()的參數應該是單個文件。您可以迭代一組文件來打開文檔。

static void Main(string[] args) 
    { 
     const string folder = "C:\\"; 

     // Loop trough all 
     foreach (var file in Directory.EnumerateFiles(folder, "*.xml")) 
     { 
      var document = XDocument.Load(file); 
     } 

     // When it should explicitly be one 
     var singleFile = Directory.GetFiles(folder, "*.xml").SingleOrDefault(); 
     if (singleFile == null) throw new Exception("File missing or multiple files found"); 
     var document = XDocument.Load(singleFile); 
    } 
2

既然您標記此信息與vb.net這裏是一個vb答案。

Dim fileList as new List(of FileInfo) 
dim basepath as string = "drive:\path\to\base\folder" 

fileList = new IO.DirectoryInfo(basepath).GetFiles("*.xml") 

For each fle in fileList 
    Dim xDoc = XDocument.Load(fle.FullName) 
    objIntegrationInfo.xmlstring += xDoc.ToString() 
Next