我的代码如下
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文件中附件文件的指示器
答案 0 :(得分:0)
是的,您只会看到写入最后一行,因为您正在创建一个新的zip文件并覆盖以前的时间。您需要将zip文件的创建和保存移至foreach (XmlNode Xn in Xe)
之外,或使用ZipFile.Open打开现有的zip文件并进行更新。
答案 1 :(得分: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个File_Name