如何将搜索结果传递给另一个函数来获取文件
static void Main(string[] args)
{
XmlDocument xml = new XmlDocument();
xml.Load(@"C:\MR.xml");
XmlNodeList stations = xml.SelectNodes("//FileDump/Message/Attachment");
var Message_ID = xml.SelectSingleNode("//FileDump/Message/MsgID").InnerXml;
Console.WriteLine("Message ID is :{0}", Message_ID);
foreach (XmlNode station in stations)
{
var File_Name = station.SelectSingleNode("FileName").InnerXml;
var File_ID = station.SelectSingleNode("FileID").InnerXml;
}
}
以下是搜索结果:
Message ID is :4830B9AA00000F7900650001
The File Name :1088656850147001.HTM
The File ID is :4830B9AB0000092207F42618.HTM
The File Name :fxdailyupdate_080519.pdf
The File ID is :4830B9AC000007F907E42798.pdf
The File Name :DR080516R.pdf
The File ID is :4830B9AD0000092207F42620.pdf
答案 0 :(得分:1)
将其作为通用的字符串列表传递:
List<string> fileNames = new List<string>();
foreach (XmlNode station in stations)
{
string File_Name = station.SelectSingleNode("FileName").InnerXml;
string File_ID = station.SelectSingleNode("FileID").InnerXml;
fileNames.Add(File_Name);
}
ShowFiles(fileNames);
然后该函数将具有:
void ShowFiles(List<string> fileNames)
{
fileNames.ForEach(fileName =>
{
//show current file...
});
}
答案 1 :(得分:0)
如果你想减少内存使用量,你可以这样做。
class example
{
static void Main(string[] args)
{
XmlDocument xml = new XmlDocument();
xml.Load(@"C:\MR.xml");
XmlNodeList stations = xml.SelectNodes("//FileDump/Message/Attachment");
var Message_ID = xml.SelectSingleNode("//FileDump/Message/MsgID").InnerXml;
Console.WriteLine("Message ID is :{0}", Message_ID);
foreach (XmlNode station in stations)
{
string File_Name = station.SelectSingleNode("FileName").InnerXml;
string File_ID = station.SelectSingleNode("FileID").InnerXml;
grabber(File_Name, File_ID);
}
}
static void grabber(string Name, string Id)
{
// grab it here
}
}
答案 2 :(得分:0)
这是我到目前为止所做的,我只能获取第一个附件文件名:
static void Main(string[] args)
{
XmlDocument xml = new XmlDocument();
xml.Load(@"C:\Temp\XML\MR.xml");
XmlNodeList Xe = xml.SelectNodes("//FileDump/Message/Attachment");
var Message_ID = xml.SelectSingleNode("//FileDump/Message/MsgID").InnerXml;
Console.WriteLine("Message ID is :{0}", Message_ID);
foreach (XmlNode Xn in Xe)
{
string File_Name = Xn.SelectSingleNode("FileName").InnerXml;
string File_ID = Xn.SelectSingleNode("FileID").InnerXml;
grabber(File_Name, File_ID);
}
}
static void grabber(string Name, string Id)
{
XmlDocument xml = new XmlDocument();
xml.Load(@"C:\Temp\XML\MR.xml");
string curFile = @"c:\Temp\att\" + xml.SelectSingleNode("//FileDump/Message/Attachment/FileName").InnerXml;
string bbgfile = @"c:\Temp\xml\" + "MR_" + xml.SelectSingleNode("//FileDump/Message/MsgID").InnerXml + ".xml";
string zipfilename = "MR_" + xml.SelectSingleNode("//FileDump/Message/MsgID").InnerXml + ".zip";
string rkzip = System.IO.Path.Combine(@"C:\Temp\ZIP\", zipfilename);
//Console.WriteLine(File.Exists(curFile) ? "Attachement File Name: " + File_Name + " exists." : "File does not exist.");
//Console.ReadLine();
string msgsave = @"c:\Temp\ZIP\" + xml.SelectSingleNode("//FileDump/Message/Attachment/FileName");
//System.IO.File.Copy(curFile, msgsave, true);
using (ZipFile zip = new ZipFile())
{
string zipFileName = System.IO.Path.Combine(@"C:\Temp\ZIP\", "MR_" + xml.SelectSingleNode("//FileDump/Message/MsgID").InnerXml + ".zip");
zip.AddFile(curFile, "");
zip.AddFile(bbgfile, "");
zip.Save(rkzip);
}
}
我错过了什么?