0

我的代码如下

static void Main(string[] args)
    {
        string startPath = @"c:\Temp\att\";
        string xmlpath = @"c:\Temp\log\";
        var files = Directory.GetFiles(xmlpath, "*.*", SearchOption.AllDirectories)
        .Where(s => s.EndsWith(".xml"));

        foreach (string xml in files)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(xml);
            XmlNodeList Xe = doc.SelectNodes("//FileDump/Message/Attachment");
            var Message_ID = doc.SelectSingleNode("//FileDump/Message/MsgID").InnerXml;
            foreach (XmlNode Xn in Xe)
            {
                var linkNode = Xn.SelectSingleNode("FileName");
                if (linkNode != null)
                {
                    string link = linkNode.InnerText.Trim();
                }
                string File_Name = Xn.SelectSingleNode("FileName").InnerXml;
                string File_ID = Xn.SelectSingleNode("FileID").InnerXml;


                //System.IO.File.Copy(curFile, msgsave, true);

                string msgsave = @"c:\Temp\ZIP\" + File_Name;
                string curFile = startPath + File_Name;
                string bbgfile = xmlpath + "MR_" + Message_ID + ".xml";
                string zipfilename = "MR_" + Message_ID + ".zip";
                string rkzip = System.IO.Path.Combine(@"C:\Temp\log\", zipfilename);

                try
                {

                    using (ZipFile zip = new ZipFile())
                    {
                        string zipFileName = System.IO.Path.Combine(@"C:\Temp\log\",  "MR_" + Message_ID + ".zip");
                        zip.AddFile(curFile, "");
                        zip.AddFile(bbgfile, "");
                        zip.Save(rkzip);
                    }

                }

问题如下: FileName 有多个文件,它只压缩一个文件,我尝试调试我的代码,它显示 3 个文件,但只有一个文件被压缩。有人可以概述我的代码有什么问题吗?FileName 是 xml 文件中附件文件的指示符

4

2 回答 2

0

Yes you will only see write for the last line because your are creating a new zip file and overwriting the previous time. You need to move the creation and saving of the zip file to outside of your foreach (XmlNode Xn in Xe) or open the existing zip file using ZipFile.Open and update it.

于 2013-10-20T03:33:04.077 回答
0
class Program
{
    static void Main(string[] args)
    {
        string startPath = @"c:\Temp\att\";
        string xmlpath = @"c:\Temp\log\";
        var files = Directory.GetFiles(xmlpath, "*.*", SearchOption.AllDirectories)
        .Where(s => s.EndsWith(".xml"));

        foreach (string xml in files)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(xml);
            XmlNodeList Xe = doc.SelectNodes("//FileDump/Message/Attachment");
            var Message_ID = doc.SelectSingleNode("//FileDump/Message/MsgID").InnerXml;
            foreach (XmlNode Xn in Xe)
            {
                var linkNode = Xn.SelectSingleNode("FileName");
                if (linkNode != null)
                {
                    string link = linkNode.InnerText.Trim();
                }
                string File_Name = Xn.SelectSingleNode("FileName").InnerXml;
                string File_ID = Xn.SelectSingleNode("FileID").InnerXml;

                //System.IO.File.Copy(curFile, msgsave, true);

            }
            try
            {
                string msgsave = @"c:\Temp\ZIP\" + doc.SelectSingleNode("//FileDump/Message/Attachment/FileName").InnerXml;
                string curFile = startPath + doc.SelectSingleNode("//FileDump/Message/Attachment/FileName").InnerXml;
                string bbgfile = xmlpath + "MR_" + Message_ID + ".xml";
                string zipfilename = "MR_" + Message_ID + ".zip";
                string rkzip = System.IO.Path.Combine(@"C:\Temp\log\", zipfilename);

                using (ZipFile zip = new ZipFile())
                {
                    string zipFileName = System.IO.Path.Combine(@"C:\Temp\log\", "MR_" + Message_ID + ".zip");
                    zip.AddFile(curFile, "");
                    zip.AddFile(bbgfile, "");
                    zip.Save(rkzip);
                }

            }

即使我在 foreach 循环之外使用了 zip,它仍然只压缩第一个 FileName,即使有 3 个 FilName

于 2013-10-20T03:55:56.860 回答