c# - Windows 窗体:从网站到 dataGridView 的 XML
问题描述
我想将此XML的特定部分直接导入我在 Windows 窗体中的 dataGridView 中,而无需下载 XML 文件。
当前工作代码,正在下载完整文件(不需要):
var wd = new WebClient();
wd.DownloadFile("https://www.aviationweather.gov/adds/dataserver_current/current/metars.cache.xml", @"c:\temp\metars.xml");
var ds = new DataSet();
ds.ReadXml(@"c:\temp\metars.xml");
dataGridView1.DataSource = ds.Tables["METAR"];
有没有办法直接从链接获取 XML 的一部分,例如当 station_id=CYDL 时的 METAR 信息到 dataGridView?
解决方案
您可以通过流下载内容,而不是像这样保存到文件中:
using var client = new HttpClient();
using var contentStream = await client.GetStreamAsync("https://www.aviationweather.gov/adds/dataserver_current/current/metars.cache.xml");
var ds = new DataSet();
ds.ReadXml(contentStream);
dataGridView1.DataSource = ds.Tables["METAR"];
或者,如果您更喜欢非异步 ( await
) 方式:
using var client = new HttpClient();
var streamTask = client.GetStreamAsync("https://www.aviationweather.gov/adds/dataserver_current/current/metars.cache.xml");
streamTask.Wait();
using var contentStream = streamTask.Result;
var ds = new DataSet();
ds.ReadXml(contentStream);
dataGridView1.DataSource = ds.Tables["METAR"];
推荐阅读
- html - 网格区域在新城市中不起作用,我该如何解决?
- terraform - 如何确定要使用的 Terraform Provider 版本?
- mysql - centos7上mysql安装初始化失败
- python - 如何拆分输入以创建字典?
- php - POST 数据不会排入 PHP 循环队列
- ios - 如何在 SwiftUI 中为我的视图提供背景图像
- bigcommerce - 如何通过 API 获取 Bigcommerce 商店的发货地址
- mysql - MySQL:如何优化在多列索引上具有“排序依据”的 SQL 脚本的分页?
- ios - 如何允许 iOS 的原生屏幕录制功能捕获 Agora 音频?
- r - 在 ODE 系统中添加 if then 语句来条件初始值;解解