如何将字符串[]中的单个值传递给XDocument.Load Stream?

时间:2015-05-12 15:45:26

标签: c# arrays linq xpath linq-to-xml

我有这段代码:

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(),以便我可以在控制台中编写所有提取。

1 个答案:

答案 0 :(得分: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.
}