2015-05-12 43 views
1

我有這樣的代碼:如何將單個值從字符串[]傳遞到XDocument.Load流?

namespace ReadXMLfromFile 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
      string path = args[0]; 

      Console.WriteLine("Looking in Directory: " + path); 
      Console.WriteLine("Files in Directory:"); 

      string[] files = Directory.GetFiles(path, "*.xml"); 
      foreach (string file in files) 
      { 
       Console.WriteLine(Path.GetFileName(file)); 
      } 

      XDocument doc = XDocument.Load(???????); 

      var spec = doc.XPathSelectElement("project/triggers/hudson.triggers.TimerTrigger/spec").Value; 

      //Write to the console 

      Console.Write(spec); 
      .... 

我正在寫一個方案,着眼於多個XML文件在一個目錄裏翻出XML元素。

我希望能夠使用字符串數組中的每個文件名值,並將它們傳遞給XDocument.Load(),以便我可以在控制檯中編寫所有提取。

+0

您的意思是說您要從所有文件加載XML嗎? –

+1

我想你想在foreach循環中移動以'XDocument'開始的三行,並且根據需要使用'XDocument.Load()'加載每個文件 – DWright

回答

0

你已經完成了大部分工作。您需要將字符串uri的一個一個地傳遞給XDocument.Load

foreach (string path in Directory.EnumerateFiles(path, "*.xml", SearchOptions.AllDirectories)) 
{ 
    XDocument doc = XDocument.Load(path); 
    var spec = doc.XPathSelectElement("project/triggers/ 
             hudson.triggers.TimerTrigger/spec").Value; 

    // Do something with spec. 
} 
+0

**完全**,謝謝。 –

+0

@OlylyMarsay您好!很高興幫助。 –

相關問題