How to determine if a string value is in XML format?

2

I'm connecting to an API, or service like that, which returns me an xml when it finds data in the database, but unfortunately it returns a string if nothing is found or an error occurs. In my application I'm using the following code to connect to this API and retrieve the data:

 WebRequest request = WebRequest.Create(_fileUrl);

 HttpWebResponse response = (HttpWebResponse) request.GetResponse();

 Stream dataStream = response.GetResponseStream();

 var reader = new StreamReader(dataStream);

 string responseFromServer = reader.ReadToEnd();

 xmlDoc.LoadXml(responseFromServer);

As you might expect when there is no data in the database, an error occurs when giving a load in xmlDoc because the responseFromServer variable is not XML but a string.

The question is: How do I identify whether my string returned an xml or not? I do not want to "" try to give a load and if it does not work I understand that what came up was a string. I may be masking other errors when doing this.     

asked by anonymous 25.02.2014 / 21:47

1 answer

4

You can check whether response.ContentType is application/xml or text/xml and use that information to make the decision. If the service is well configured, it should send this header (and send text/plain to the error message).

    
25.02.2014 / 21:54