2016-12-27 355 views
0

我想解壓和解析位於here下載並解壓縮XML文件

這裏的XML文件是我的代碼:

HttpClientHandler handler = new HttpClientHandler() 
{ 
    CookieContainer = new CookieContainer(), 
    UseCookies = true, 
    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, 
    // | DecompressionMethods.None, 

}; 

using (var http = new HttpClient(handler)) 
{ 

    var response = 
     http.GetAsync(@"https://login.tradedoubler.com/report/published/aAffiliateEventBreakdownReportWithPLC_806880712_4446152766894956100.xml.zip").Result; 

    Stream streamContent = response.Content.ReadAsStreamAsync().Result; 

    using (var gZipStream = new GZipStream(streamContent, CompressionMode.Decompress)) 
    { 
     var settings = new XmlReaderSettings() 
     { 
      DtdProcessing = DtdProcessing.Ignore 
     }; 

     var reader = XmlReader.Create(gZipStream, settings); 
     reader.MoveToContent(); 

     XElement root = XElement.ReadFrom(reader) as XElement; 
    } 
} 

我得到一個異常的XmlReader.Create(gZipStream,設置)

GZip標頭中的幻數不正確。請確保您在一個gzip料流通

要仔細檢查,我從網上獲取正確格式化的數據,我搶流,並將其保存到一個文件:

byte[] byteContent = response.Content.ReadAsByteArrayAsync().Result; 
File.WriteAllBytes(@"C:\\temp\1111.zip", byteContent); 

我考察1111後.zip,它顯示爲格式良好的zip文件,並帶有我需要的xml。

,我被告知here,我並不需要GZipStream可言,但如果我從代碼完全刪除壓縮數據流,並通過streamContent直接向XML閱讀器,我得到一個異常:

「數據在根級別無效,行1,位置1「。

無論是壓縮或不壓縮,我仍然無法解析此文件。我究竟做錯了什麼?

回答

1

在保存流本地文件夾中,用的ZipFile類解壓縮。 類似這樣的:

byte[] byteContent = response.Content.ReadAsByteArrayAsync().Result; 
    string filename = @"C:\temp\1111.zip"; 
    File.WriteAllBytes(filename, byteContent); 

    string destinationDir = @"c:\temp"; 
    string xmlFilename = "report.xml"; 

    System.IO.Compression.ZipFile.ExtractToDirectory(filename, destinationDir); 

    XmlDocument xmlDoc = new XmlDocument(); 
    xmlDoc.Load(Path.Combine(destinationDir, xmlFilename)); 

    //xml reading goes here...