EDN Admin
Well-known member
Hi
I have the following XML content returned and i have stored it in a string .But i am not able to load it with XDocument .
public void startshere()
{
HttpWebRequest myHttpWebRequest1 = WebRequest.CreateHttp(uri);
<br/>
<br/>
// Create an instance of the RequestState and assign the previous myHttpWebRequest1<br/>
// object to its request field.
<br/>
RequestState myRequestState = new RequestState();<br/>
myRequestState.request = myHttpWebRequest1;
<br/>
// Start the asynchronous request.<br/>
IAsyncResult result = (IAsyncResult)myHttpWebRequest1.BeginGetResponse(new AsyncCallback(RespCallback), myRequestState);
}<br/>
catch<br/>
{ }
<br/>
}<br/>
private static void RespCallback(IAsyncResult asynchronousResult)<br/>
{<br/>
try<br/>
{<br/>
// State of request is asynchronous.<br/>
RequestState myRequestState = (RequestState)asynchronousResult.AsyncState;<br/>
HttpWebRequest myHttpWebRequest2 = myRequestState.request;<br/>
myRequestState.response = (HttpWebResponse)myHttpWebRequest2.EndGetResponse(asynchronousResult);
// Read the response into a Stream object.<br/>
Stream responseStream = myRequestState.response.GetResponseStream();<br/>
myRequestState.streamResponse = responseStream;
// Begin the Reading of the contents of the HTML page and print it to the console.
<br/>
IAsyncResult asynchronousInputRead = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);<br/>
}<br/>
catch (WebException e)<br/>
{<br/>
// Need to handle the exception<br/>
}<br/>
}<br/>
private static void ReadCallBack(IAsyncResult asyncResult)<br/>
{<br/>
try<br/>
{
RequestState myRequestState = (RequestState)asyncResult.AsyncState;<br/>
Stream responseStream = myRequestState.streamResponse;<br/>
int read = responseStream.EndRead(asyncResult);<br/>
// Read the HTML page and then do something with it<br/>
if (read > 0)<br/>
{<br/>
myRequestState.requestData.Append(Encoding.UTF8.GetString(myRequestState.BufferRead, 0, read));<br/>
IAsyncResult asynchronousResult = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);<br/>
}<br/>
else<br/>
{<br/>
if (myRequestState.requestData.Length > 1)<br/>
{<br/>
// do something with the response stream here<br/>
string stringContent;<br/>
stringContent = myRequestState.requestData.ToString();<br/>
//XDocument.<br/>
XDocument docXMLData = XDocument.Load(stringContent);
At the above statement i get the XmlException with error: cannot file like "<below xml content>"
if i use XDocument docXMLData = XDocument.Parse(stringContent); it gives me the error : "Data at the root level is invalid. Line 1, position 1".
Can anyone suggest how to load the below xml data and parse it ?
<?xml version="1.0" encoding="UTF-8"?><br/>
-<Response xmlns=" http://schemas.microsoft.com/search/local/ws/rest/v1 http://schemas.microsoft.com/search/local/ws/rest/v1 " xmlns:xsd=" http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance <Copyright>Copyright http://www.w3.org/2001/XMLSchema-instance <Copyright>Copyright
© 2012 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.</Copyright><BrandLogoUri>http://dev.virtualearth.net/Branding/logo_powered_by.png</BrandLogoUri><StatusCode>200</StatusCode><StatusDescription>OK</StatusDescription><AuthenticationResultCode>ValidCredentials</AuthenticationResultCode><TraceId>d9a7d888ad974911a689401a9da1f7d0|BAYM001212|02.00.138.500|BY2MSNVM000009,
BY2MSNVM001083</TraceId>-<ResourceSets>-<ResourceSet><EstimatedTotal>1</EstimatedTotal>-<Resources>-<Location><Name>1 Microsoft Way, Redmond, WA 98052</Name>-<Point><Latitude>47.640570402145386</Latitude><Longitude>-122.12937377393246</Longitude></Point>-<BoundingBox><SouthLatitude>47.636707684574709</SouthLatitude><WestLongitude>-122.13701709146854</WestLongitude><NorthLatitude>47.644433119716062</NorthLatitude><EastLongitude>-122.12173045639638</EastLongitude></BoundingBox><EntityType>Address</EntityType>-<Address><AddressLine>1
Microsoft Way</AddressLine><AdminDistrict>WA</AdminDistrict><AdminDistrict2>King Co.</AdminDistrict2><CountryRegion>United States</CountryRegion><FormattedAddress>1 Microsoft Way, Redmond, WA 98052</FormattedAddress><Locality>Redmond</Locality><PostalCode>98052</PostalCode></Address><Confidence>Medium</Confidence><MatchCode>Good</MatchCode>-<GeocodePoint><Latitude>47.640570402145386</Latitude><Longitude>-122.12937377393246</Longitude><CalculationMethod>Interpolation</CalculationMethod><UsageType>Display</UsageType><UsageType>Route</UsageType></GeocodePoint></Location></Resources></ResourceSet></ResourceSets></Response>
View the full article
I have the following XML content returned and i have stored it in a string .But i am not able to load it with XDocument .
public void startshere()
{
HttpWebRequest myHttpWebRequest1 = WebRequest.CreateHttp(uri);
<br/>
<br/>
// Create an instance of the RequestState and assign the previous myHttpWebRequest1<br/>
// object to its request field.
<br/>
RequestState myRequestState = new RequestState();<br/>
myRequestState.request = myHttpWebRequest1;
<br/>
// Start the asynchronous request.<br/>
IAsyncResult result = (IAsyncResult)myHttpWebRequest1.BeginGetResponse(new AsyncCallback(RespCallback), myRequestState);
}<br/>
catch<br/>
{ }
<br/>
}<br/>
private static void RespCallback(IAsyncResult asynchronousResult)<br/>
{<br/>
try<br/>
{<br/>
// State of request is asynchronous.<br/>
RequestState myRequestState = (RequestState)asynchronousResult.AsyncState;<br/>
HttpWebRequest myHttpWebRequest2 = myRequestState.request;<br/>
myRequestState.response = (HttpWebResponse)myHttpWebRequest2.EndGetResponse(asynchronousResult);
// Read the response into a Stream object.<br/>
Stream responseStream = myRequestState.response.GetResponseStream();<br/>
myRequestState.streamResponse = responseStream;
// Begin the Reading of the contents of the HTML page and print it to the console.
<br/>
IAsyncResult asynchronousInputRead = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);<br/>
}<br/>
catch (WebException e)<br/>
{<br/>
// Need to handle the exception<br/>
}<br/>
}<br/>
private static void ReadCallBack(IAsyncResult asyncResult)<br/>
{<br/>
try<br/>
{
RequestState myRequestState = (RequestState)asyncResult.AsyncState;<br/>
Stream responseStream = myRequestState.streamResponse;<br/>
int read = responseStream.EndRead(asyncResult);<br/>
// Read the HTML page and then do something with it<br/>
if (read > 0)<br/>
{<br/>
myRequestState.requestData.Append(Encoding.UTF8.GetString(myRequestState.BufferRead, 0, read));<br/>
IAsyncResult asynchronousResult = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);<br/>
}<br/>
else<br/>
{<br/>
if (myRequestState.requestData.Length > 1)<br/>
{<br/>
// do something with the response stream here<br/>
string stringContent;<br/>
stringContent = myRequestState.requestData.ToString();<br/>
//XDocument.<br/>
XDocument docXMLData = XDocument.Load(stringContent);
At the above statement i get the XmlException with error: cannot file like "<below xml content>"
if i use XDocument docXMLData = XDocument.Parse(stringContent); it gives me the error : "Data at the root level is invalid. Line 1, position 1".
Can anyone suggest how to load the below xml data and parse it ?
<?xml version="1.0" encoding="UTF-8"?><br/>
-<Response xmlns=" http://schemas.microsoft.com/search/local/ws/rest/v1 http://schemas.microsoft.com/search/local/ws/rest/v1 " xmlns:xsd=" http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance <Copyright>Copyright http://www.w3.org/2001/XMLSchema-instance <Copyright>Copyright
© 2012 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.</Copyright><BrandLogoUri>http://dev.virtualearth.net/Branding/logo_powered_by.png</BrandLogoUri><StatusCode>200</StatusCode><StatusDescription>OK</StatusDescription><AuthenticationResultCode>ValidCredentials</AuthenticationResultCode><TraceId>d9a7d888ad974911a689401a9da1f7d0|BAYM001212|02.00.138.500|BY2MSNVM000009,
BY2MSNVM001083</TraceId>-<ResourceSets>-<ResourceSet><EstimatedTotal>1</EstimatedTotal>-<Resources>-<Location><Name>1 Microsoft Way, Redmond, WA 98052</Name>-<Point><Latitude>47.640570402145386</Latitude><Longitude>-122.12937377393246</Longitude></Point>-<BoundingBox><SouthLatitude>47.636707684574709</SouthLatitude><WestLongitude>-122.13701709146854</WestLongitude><NorthLatitude>47.644433119716062</NorthLatitude><EastLongitude>-122.12173045639638</EastLongitude></BoundingBox><EntityType>Address</EntityType>-<Address><AddressLine>1
Microsoft Way</AddressLine><AdminDistrict>WA</AdminDistrict><AdminDistrict2>King Co.</AdminDistrict2><CountryRegion>United States</CountryRegion><FormattedAddress>1 Microsoft Way, Redmond, WA 98052</FormattedAddress><Locality>Redmond</Locality><PostalCode>98052</PostalCode></Address><Confidence>Medium</Confidence><MatchCode>Good</MatchCode>-<GeocodePoint><Latitude>47.640570402145386</Latitude><Longitude>-122.12937377393246</Longitude><CalculationMethod>Interpolation</CalculationMethod><UsageType>Display</UsageType><UsageType>Route</UsageType></GeocodePoint></Location></Resources></ResourceSet></ResourceSets></Response>
View the full article