2011-03-02 77 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");