您的位置:首页 > 编程语言 > C#

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

2016-06-21 14:28 465 查看
string sourceFilePath = @"E:\文件拷贝\xx\3773\3773.zip";

FileInfo fileInfo = new FileInfo(sourceFilePath);
long length = fileInfo.Length;

if (length == 0)
{
return;
}

using (ZipInputStream zip = new ZipInputStream(File.OpenRead(sourceFilePath)))
{
ZipEntry theEntry;
string destinationDirPath = @"\\172.21.3.21\liung\";
while ((theEntry = zip.GetNextEntry()) != null)
{
destinationDirPath += theEntry.Name;

using (FileStream streamWriter = new FileStream(destinationDirPath, FileMode.Create, FileAccess.Write, FileShare.Read | FileShare.Write))
{
byte[] bytes = new byte[zip.Length];
zip.Read(bytes, 0, bytes.Length);
streamWriter.Write(bytes, 0, bytes.Length);
}

XmlTextReader xmlReader = new XmlTextReader(zip);
while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name.ToLower() == "fileheader")
{
string reportTime = xmlReader.GetAttribute("reportTime").Replace("24:00:00", "23:59:59");
string startTime = xmlReader.GetAttribute("startTime").Replace("24:00:00", "23:59:59");
string endTime = xmlReader.GetAttribute("endTime").Replace("24:00:00", "23:59:59");

break;
}
}
}
}

Console.ReadKey();

using (GZipInputStream zip = new GZipInputStream(File.OpenRead(sourceFilePath)))
{
using (XmlTextReader xmlReader = new XmlTextReader(zip))
{
while (xmlReader.Read())
{
// hw没有period,暂时给一个默认值15
if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name.ToLower() == "fileheader")
{
string reportTime = xmlReader.GetAttribute("reportTime").Replace("24:00:00", "23:59:59");
string startTime = xmlReader.GetAttribute("startTime").Replace("24:00:00", "23:59:59");
string endTime = xmlReader.GetAttribute("endTime").Replace("24:00:00", "23:59:59");

break;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: