I am completely new to API data retreival, and would appreciate some help. This is the code I have at the moment:
else if (e.KeyCode == Keys.Enter & InputTextbox.Text.Contains("hot"))
{
try
{
XElement doc = XElement.Load("https://api.forecast.io/forecast/*APIKEY*/-31.4296,152.9082");
OutputTextbox.Text = "It is currently " + doc;
pBuilder.ClearContent();
pBuilder.AppendText(OutputTextbox.Text);
sSynth.Speak
pBuilder);
e.SuppressKeyPress = true;
InputTextbox.Text = "";
}
catch (System.Xml.XmlException fe)
{
MessageBox.Show(fe.Message);
}
This gives back the error message: "Data at the root level is invalid. Line 1, position 1."
Can someone please let me know where I am going wrong?
First, you need to see what the output of the api call is, try:
using(var client = new WebClient()) {
var responseStr = client.DownloadString("https://api.forecast.io/forecast/*APIKEY*/-31.4296,152.9082");
OutputTextbox.Text = responseStr;
}
Then, to load this xml with XElement, it needs to be completely valid XML. That's the source of your error message: XElement is very strict. If the response is HTML, consider using HtmlAgilityPack, it will save your sanity.
var doc = new HtmlDocument();
doc.Load("https://....");
If it's a json api or something like that, consider using ServiceStack. That will also save your sanity.
Good luck.