0

如何将搜索结果传递给另一个函数来抓取文件

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
4

3 回答 3

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...
    });
}
于 2013-11-03T12:07:16.990 回答
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);
        }
    }

我错过了什么?

于 2013-11-04T01:46:12.313 回答
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
    }
}
于 2013-11-03T12:09:17.890 回答