将 XML 提要读取到 XElement 中
我有一个 Xml 流,我想将其读入 XElement
中。我见过使用 XmlTextReader
的示例,但我需要在 XElement
中使用它。
到目前为止我拥有的代码:
string url =
String.Format( "http://dev.virtualearth.net/REST/v1/Locations/{0}?o=xml&key={1}", HttpUtility.UrlEncode( AddressQuery ), mapkey );
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
XmlTextReader reader = new XmlTextReader( url );
我只是不确定如何让读者进入 XElement。也许我的处理方式是错误的。
I have an Xml Stream that I'd like to read into an XElement
. I've seen samples that use XmlTextReader
but I need it in an XElement
.
The code that I have so far:
string url =
String.Format( "http://dev.virtualearth.net/REST/v1/Locations/{0}?o=xml&key={1}", HttpUtility.UrlEncode( AddressQuery ), mapkey );
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
XmlTextReader reader = new XmlTextReader( url );
I'm just not sure how to get the reader into an XElement. Perhaps I'm going about it the wrong way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 linq to xml 你可以简单地做到这一点
with linq to xml you can simply do this
您仅创建了一个
WebRequest
实例 - 这实际上并不要求服务器下载 URL 的内容。调用WebRequest.GetResponse()
应从服务器下载 URL 的内容。 WebRequest 的 MSDN 页面 提供了下载URL 的内容。收到响应后,您可以调用 XDocument.Load() 并将响应流传递给它(通过从响应对象调用
GetResponseStream()
)。XDocument
类具有检索 XML 文档中的XElement
的方法。You've only created an instance of
WebRequest
- this doesn't actually ask the server to download the contents of the URL. CallingWebRequest.GetResponse()
should download the contents of the URL from the server. The MSDN page for WebRequest has an example of downloading the contents of a URL.Once you have the response, you can call XDocument.Load() and pass it the response stream (by calling
GetResponseStream()
from the response object). TheXDocument
class has methods to retrieve anXElement
in the XML document.