简体   繁体   中英

How can I pass individual values from a string[] to the XDocument.Load Stream?

I have this code:

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);
            ....

I'm writing a program that looks at multiple XML files in a single directory and pulls out XML Elements.

I want to be able to use each filename value in the string array and pass them to the XDocument.Load() so that I can write all the extractions in the console.

You've done most of the work already. You need to pass the string uri's one by one to 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.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM