【问题标题】:How to get XML from WebRequest in C#?如何在 C# 中从 WebRequest 获取 XML?
【发布时间】:2012-12-02 19:50:52
【问题描述】:

我正在尝试使用以下代码从 URL 获取 XML 数据:

WebRequest webRequest = WebRequest.Create(Url);
using (WebResponse webResponse = await webRequest.GetResponseAsync())
{
    using (Stream responseStream = webResponse.GetResponseStream())
    {
        XmlDocument XmlDoc = new XmlDocument();
        XmlDoc.Load(webResponse); ////error
    }
}

Visual Studio 既不接受“XMLDoc.Load()”也不接受“XMLDoc.LoadXml()”——那么,如何从 webrequest 中获取 xmldoc? 谢谢

【问题讨论】:

  • 不需要WebRequest。试试 XDocument.Load(url)
  • XmlDoc.Load(webResponse); 应该是XmlDoc.Load(responseStream);
  • @L.B - 问题是,没有 *.xml 我尝试获取在输出中提供 XML 的页面。
  • @nick_w - 没有区别。他不喜欢负载:/
  • 好的,出现同样的问题,VisualStudio 不知道 Load! XDoc.Load(URL) 真的能获取到 URL 目标的内容吗?即使不是直接 *.xml ?

标签: c# xml windows-8 stream webrequest


【解决方案1】:

这应该可行:

string url = @"http://ws.audioscrobbler.com/2.0/?method=geo.getevents&location=" + ub.Ciudad + "&page=1&api_key=" + LAST_API_KEY; //for example
XmlDocument xDoc = await XmlDocument.LoadFromUriAsync(new Uri(url)); //this does the web request

【讨论】:

  • 只要您不必编辑标题,它就可以工作。这些天很多 API 需要你编辑标题。
猜你喜欢
  • 2020-05-02
  • 1970-01-01
  • 1970-01-01
  • 2019-01-26
  • 1970-01-01
  • 1970-01-01
  • 2020-02-25
  • 2020-03-18
  • 1970-01-01
相关资源
最近更新 更多