开发者

Reading an XML Feed into XElement

开发者 https://www.devze.com 2023-03-12 09:07 出处:网络
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.

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 Xm开发者_运维问答lTextReader( url );

I'm just not sure how to get the reader into an XElement. Perhaps I'm going about it the wrong way.


with linq to xml you can simply do this

var xml = XElement.Load(uri);


You've only created an instance of WebRequest - this doesn't actually ask the server to download the contents of the URL. Calling WebRequest.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). The XDocument class has methods to retrieve an XElement in the XML document.

0

精彩评论

暂无评论...
验证码 换一张
取 消