Reading an XML stream in XElement

I have an Xml Stream that I would like to read in XElement . I have seen samples that use XmlTextReader , but I need this in XElement .

The code I have so far is:

 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 to enter XElement. Maybe I'm wrong.

+4
source share
2 answers

with linq in xml you can just do it

 var xml = XElement.Load(uri); 
+8
source

You only created an instance of WebRequest - this does not actually ask the server to download the contents of the URL. A call to WebRequest.GetResponse() should load the contents of the URL from the server. The MSDN page for WebRequest provides an example of loading the contents of a URL.

After the response, you can call XDocument.Load () and pass a response stream to it (by calling GetResponseStream() from the response object). The XDocument class has methods for extracting an XElement in an XML document.

+4
source

All Articles