从网络下载XML文件后无法读取 [英] Can't read an XML file after downloading it from web

查看:86
本文介绍了从网络下载XML文件后无法读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试下载文件以供xmlreader使用.该文件可以很好地下载,但不会让我阅读.

这是我到目前为止的内容:

Hi everyone,

I am trying to download a file for xmlreader purposes. The file can download fine, but it won''t let me read it.

This is what I have so far:

public void downloadXmlFile()
        {
            WebClient webClient = new WebClient();
            //webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
            webClient.DownloadFileAsync(new Uri("http://mysite", "c:\newxmlfile.xml");
        }



这是我尝试阅读的方式:



This is how I try to read it:

try
            {
                reader2 = new XmlTextReader(xmlUrl2);
                reader2.MoveToContent();
            }



它不喜欢新的XmlTextReader(xmlUrl2),它是字符串"C:\ newxmlfile.xml".

我缺少明显的东西吗?我收到的错误消息是:
该进程无法访问文件"C:\ newxmlfile.xml",因为该文件正在被另一个进程使用.



It''s not liking the new XmlTextReader(xmlUrl2) which is the string of "C:\newxmlfile.xml".

Am I missing something obvious? The error message I''m getting is:
The process cannot access the file ''C:\newxmlfile.xml'' because it is being used by another process.

推荐答案

普通英语.您写入文件的代码并未关闭,因此已锁定.
The error means what it says, in plain English. your code that wrote the file, did not close it, so it is locked.


DownloadFileAsync会在启动后立即返回,而不是等待下载完成.

因此,当您尝试读取文件时,它可能仍在下载文件,这意味着该进程正在使用该文件.

使用DownloadFile或等待任务完成.
DownloadFileAsync returns as soon as it''s started, instead of waiting until the download is complete.

Therefore it may still be downloading the file when you try to read it, which would mean the file is used by that process.

Either use DownloadFile or wait for the task to finish.


这篇关于从网络下载XML文件后无法读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆