I'm pulling some xml from a web api and trying to deserialize it to a Forecast object (which I defined using the "paste xml as classes"), but I'm getting the error "Error in XML Document (1,1)". Here is my code:
public static Forecast getWeather(int zip)
{
string url = "http://api.weatherunlocked.com/api/forecast/us." + zip.ToString() + "?app_id={myId}&app_key={mykey}";
using (WebClient client = new WebClient())
{
string xml = client.DownloadString(url);
var reader = new StringReader(xml);
var serializer = new XmlSerializer(typeof(Forecast));
var instance = (Forecast)serializer.Deserialize(reader);
return instance;
}
}
When I pasted the XML as classes, it said there were some errors in the XML; I had to take out a few < characters that shouldn't be there. Is it possible that, since I cannot actually change the XML from the api call, these extra characters are making it invalid XML, and so it's never going to work? The XML is over 2000 lines so I don't think it'd be reasonable to try to go through the string and take out all of those characters. Or, is the problem something else in my code that's fixable? If anyone else has used the Weather Unlocked api or had this same problem, some sample code would be awesome.