C# 如何将单个值从字符串[]传递到XDocument.Load流?
我有以下代码:C# 如何将单个值从字符串[]传递到XDocument.Load流?,c#,arrays,linq,xpath,linq-to-xml,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:");
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(),这样我就可以在控制台中编写所有提取。您已经完成了大部分工作。您需要将字符串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.
}
您的意思是要从所有文件中加载XML吗?我想您应该将以
XDocument
开头的三行移到foreach循环中,让XDocument.load()加载每个文件