C# xml压缩包不解压的情况下解析xml内容

本文展示了一个使用C#实现的从ZIP文件中读取并解压文件的例子,并对解压后的XML文件进行特定属性的读取及处理。该过程涉及到了文件路径设置、文件长度检查、ZIP文件流操作、XML节点的遍历与属性提取等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 string sourceFilePath = @"E:\文件拷贝\xx\3773\3773.zip";
 2 
 3 FileInfo fileInfo = new FileInfo(sourceFilePath);
 4 long length = fileInfo.Length;
 5 
 6 if (length == 0)
 7 {
 8     return;
 9 }
10 
11 using (ZipInputStream zip = new ZipInputStream(File.OpenRead(sourceFilePath)))
12 {
13    ZipEntry theEntry;
14     string destinationDirPath = @"\\172.21.3.21\liung\";
15     while ((theEntry = zip.GetNextEntry()) != null)
16     {
17         destinationDirPath += theEntry.Name;
18 
19         using (FileStream streamWriter = new FileStream(destinationDirPath, FileMode.Create, FileAccess.Write, FileShare.Read | FileShare.Write))
20         {
21             byte[] bytes = new byte[zip.Length];
22             zip.Read(bytes, 0, bytes.Length);
23             streamWriter.Write(bytes, 0, bytes.Length);
24         }
25 
26         XmlTextReader xmlReader = new XmlTextReader(zip);
27         while (xmlReader.Read())
28         {
29             if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name.ToLower() == "fileheader")
30             {
31                 string reportTime = xmlReader.GetAttribute("reportTime").Replace("24:00:00", "23:59:59");
32                 string startTime = xmlReader.GetAttribute("startTime").Replace("24:00:00", "23:59:59");
33                 string endTime = xmlReader.GetAttribute("endTime").Replace("24:00:00", "23:59:59");
34 
35                 break;
36             }
37         }
38     }
39 }
40 
41 Console.ReadKey();
42 
43 using (GZipInputStream zip = new GZipInputStream(File.OpenRead(sourceFilePath)))
44 {
45     using (XmlTextReader xmlReader = new XmlTextReader(zip))
46     {
47         while (xmlReader.Read())
48         {
49             // hw没有period,暂时给一个默认值15
50             if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name.ToLower() == "fileheader")
51             {
52                 string reportTime = xmlReader.GetAttribute("reportTime").Replace("24:00:00", "23:59:59");
53                 string startTime = xmlReader.GetAttribute("startTime").Replace("24:00:00", "23:59:59");
54                 string endTime = xmlReader.GetAttribute("endTime").Replace("24:00:00", "23:59:59");
55 
56                 break;
57             }
58         }
59     }
60 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值