2011-03-02 65 views
0

我需要從安全鏈接下載xml文件,並將內容存儲到數據庫中。如何處理下載流

我可以使用文本閱讀器?或者我需要先將文件存儲到本地文件系統中,然後從我的文件系統讀取內容並存儲到我的數據庫中?

HttpWebRequest downloadRequest = (HttpWebRequest)WebRequest.Create("https://line-to-xml-file.xml"); 
string Content; 

       downloadRequest.Credentials = new NetworkCredential() 
               { 
                UserName = this._userCredentials.UserName, 
                Password = this._userCredentials.Password 
               }; 

       downloadRequest.PreAuthenticate = true; 

       using (HttpWebResponse downloadHTTPResponse = (HttpWebResponse)downloadRequest.GetResponse()) 
       { 
        using (Stream downloadResponseStream = downloadHTTPResponse.GetResponseStream()) 
        using (TextReader tReader = new StreamReader(downloadResponseStream)) 
        { 
         Content = tReader.ReadToEnd(); 
        } 
       } 
       return Content; 

由於遠程文件很大,高達100MB,所以我從調試中什麼都看不到。 當我嘗試保存它。

using (TransactionScope trans = new TransactionScope()) <--- when comes to this line, exception throws... 
{ 
// perform update, save the content into databse 
// send a notification message to message bus, indicate content has been updated 
} 

它抱怨MSDTC事務超時/取消

回答

1

通過讓它成流,你應該罰款......如果你有特定流的任何問題,那麼你可以使用MemoryStream,而不是一個FileStream的並以相同的方式使用它。但我毫不懷疑這是你的情況。

我想你也應該確保右側開放之前,你保存流的連接,當你已經使其完全加載中...您也可以與您的Command.TimeOut屬性發揮,如果它正在真的,真的長保存,這裏「值爲0表示沒有限制」,但應該避免。 但是

0

有一個XmlReader,但是如果Xml已經格式良好,並且您不需要解析它,因爲您的數據庫只是將其作爲blob,或者數據庫引擎將要解析它,你可以使用任何東西。

取決於您的數據庫架構。

聲音就像數據庫插入時有問題,但需要更多信息。

0

如何使用WebClient及其下載文件的方法。只需在本地保存,使用它並在完成使用後刪除。

0

100MB的文本是一個很大的表上補習班。你的聲明幾乎肯定會超時。檢查你的環境和你的SQL命令對象(如果有的話),並增加超時值。

0

你可能會設置一個較長的超時時間來解決超時問題,並確保你調用功能齊全

using (TransactionScope trans = new TransactionScope(
     TransactionScopeOption.Required, new TimeSpan(1, 4, 3))) // Sets time out to 1 hour 4 minutes and 3 seconds 
{ 
    // perform update, save the content into databse 
    // send a notification message to message bus, indicate content has been updated 

    trans.Complete(); 
} 

用於讀取文件,你可以可能使用Web客戶端。 WebClient允許您監視進度。

WebClient wc = new WebClient(); 
    wc.Credentials = new NetworkCredential() 
    { 
     UserName = this._userCredentials.UserName, 
     Password = this._userCredentials.Password 
    }; 

    wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged); 
    wc.DownloadFile("https://line-to-xml-file.xml", "C:\\local.xml"); 

處理程序可以有需要時進行登錄的進展:

void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
    { 
     // Log or show the current progress (e.ProgressPercentage or e.BytesReceived) 
    } 

,如果你想在字符串你可以使用,而不是下載文件DownloadString直而無需再次讀出該文件。

wc.DownloadString("https://line-to-xml-file.xml");