C#XML从XML标记

时间:2017-11-05 19:13:11

标签: c# xml

在我的XML文件中,我有标记:

 <screens>http://www.bigfoto.com/stones-background.jpg, https://www.bensound.com/bensound-img/betterdays.jpg</screens>

以及如何从此链接下载图片?

我试过了:

linkToDownload = result.Element("screens").Value.ToString();

            List<string> adress = new List<string>() { linkToDownload };

        foreach (string img in adress)
        {
            var webClient = new WebClient();

            var fname = Path.GetTempFileName();

            await webClient.DownloadFileTaskAsync(img, fname);
            Bitmap bitmap = new Bitmap(fname);

            imageList1.Images.Add(bitmap);
            listCount++;

        }

1 个答案:

答案 0 :(得分:0)

我已经测试了以下代码并且工作但是这不是asyc。首先是将图像列表拆分为数组,然后运行GetTempFileName()并更改&#34; tmp&#34;通过链接中图像的扩展进行扩展,以便Bitmap构造函数知道临时文件的内容是什么类型,并且能够创建位图对象。

var result = XDocument.Parse("<screens>http://www.bigfoto.com/stones-background.jpg,"
+ "https://www.bensound.com/bensound-img/betterdays.jpg</screens>");
var screens = result.Element("screens");
string[] links = screens==null ? new string[]{} : screens.Value.Split(',');
foreach (string link in links)
{
    var webClient = new WebClient();
    string extension = Path.GetExtension(link);
    var fileName = Path.ChangeExtension(Path.GetTempFileName(), extension);
    webClient.DownloadFile(link, fileName);
    Bitmap bitmap = new Bitmap(fileName);

    //imageList1.Images.Add(bitmap);
    //listCount++;
}