It took me a bit of trial and error but I've got it. In C# make sure you are using - using System.Xml;
Here is the code using wunderground API. In order for this to work make sure you sign up for a key other wise it will not work. Where is say this your_key that is where you put in your key. It should look like something like this. I used a button and three labels to display the information.
namespace wfats2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
XmlDocument doc1 = new XmlDocument();
doc1.Load("http://api.wunderground.com/api/your_key/conditions/q/92135.xml");
XmlElement root = doc1.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/response/current_observation");
foreach (XmlNode node in nodes)
{
string tempf = node["temp_f"].InnerText;
string tempc = node["temp_c"].InnerText;
string feels = node["feelslike_f"].InnerText;
label2.Text = tempf;
label4.Text = tempc;
label6.Text = feels;
}
}
}
}
When you press the button you will get the information displayed in the labels assign. I am still experimenting and you are able to have some sort of refresh every so often instead of pressing the button every time to get an update.
Answer from locoss on Stack OverflowIt took me a bit of trial and error but I've got it. In C# make sure you are using - using System.Xml;
Here is the code using wunderground API. In order for this to work make sure you sign up for a key other wise it will not work. Where is say this your_key that is where you put in your key. It should look like something like this. I used a button and three labels to display the information.
namespace wfats2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
XmlDocument doc1 = new XmlDocument();
doc1.Load("http://api.wunderground.com/api/your_key/conditions/q/92135.xml");
XmlElement root = doc1.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/response/current_observation");
foreach (XmlNode node in nodes)
{
string tempf = node["temp_f"].InnerText;
string tempc = node["temp_c"].InnerText;
string feels = node["feelslike_f"].InnerText;
label2.Text = tempf;
label4.Text = tempc;
label6.Text = feels;
}
}
}
}
When you press the button you will get the information displayed in the labels assign. I am still experimenting and you are able to have some sort of refresh every so often instead of pressing the button every time to get an update.
First off yeah you need to give more information in your question but off hand I can see that you have "your_key" inside of your URL. You are probably needing to replace that with your API key for this to work.
Libcurl has pure C API. Expat and libxml are written in pure C too.
You can use Windows Internet (WinINet) or Windows HTTP Services for download. WinHTTP vs. WinINet offers an overview to help you decide, which API to use.
To parse XML content, you can use MSXML. It is Microsoft's XML parser (both DOM and SAX), that is exposed as a COM interface. Using COM from C is somewhat verbose, but fairly straight forward.
When using either of the above, you do not need to ship any additional libraries with your application.
NOTE: You're really better off using
XDocumentfor most XML parsing needs nowadays.
It's telling you that the value of m_strFilePath is not valid XML. Try:
string m_strFilePath = "http://www.google.com/ig/api?weather=12414&hl=it";
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(m_strFilePath); //Load NOT LoadXml
However, this is failing (for unknown reason... seems to be choking on the à of Umidità). The following works (still trying to figure out what the difference is though):
var m_strFilePath = "http://www.google.com/ig/api?weather=12414&hl=it";
string xmlStr;
using(var wc = new WebClient())
{
xmlStr = wc.DownloadString(m_strFilePath);
}
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);
You need to use Load() instead of LoadXML(). LoadXML tries to parse a string into XML, in this case your URL.
XML Elements cannot have spaces in their names. Try to remove them first
First download the XML. Then Use like,
try {
//read xml
XmlDocument xdoc = new XmlDocument();
xdoc.Load("XMLFilePath");
XmlNodeList nodes = xdoc.SelectNodes(@"rss/channel/item");
foreach (XmlNode node in nodes)
{
XmlNode titleNode = node.SelectSingleNode("title");
string title = titleNode == null ? string.Empty : titleNode.InnerText;
};
}
After fixing the bug by setting the char* to const as in:
char const* c_url = "http://some_url.xml";
Your code works fine for me.
However you don't always want to write the data you get receive disk. Sometimes you just want to keep it in memory to use it on the fly.
Here, I wrote a function to download the target of a URL into a std::string that you can do whatever you want with. I also made the code exception safe and generally safeer using a smart pointer.
// write the data into a `std::string` rather than to a file.
std::size_t write_data(void* buf, std::size_t size, std::size_t nmemb,
void* userp)
{
if(auto sp = static_cast<std::string*>(userp))
{
sp->append(static_cast<char*>(buf), size * nmemb);
return size * nmemb;
}
return 0;
}
// To make the function thread safe you can use a smart pointer to
// hold your CURL session pointer.
// A deleter to use in the smart pointer for automatic cleanup
struct curl_dter{void operator()(CURL* curl) const
{ if(curl) curl_easy_cleanup(curl); }};
// A smart pointer to automatically clean up out CURL session
using curl_uptr = std::unique_ptr<CURL, curl_dter>;
// download the URL into a `std::string`.
std::string get_url(std::string const& url)
{
std::string data;
if(auto curl = curl_uptr(curl_easy_init()))
{
curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
curl_easy_setopt(curl.get(), CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &data);
CURLcode ec;
if((ec = curl_easy_perform(curl.get())) != CURLE_OK)
throw std::runtime_error(curl_easy_strerror(ec));
}
return data;
}
int main()
{
curl_global_init(CURL_GLOBAL_DEFAULT);
auto xml = get_url("http://google.co.uk");
std::cout << xml << '\n';
curl_global_cleanup();
}
Note: I also added the CURLOPT_FOLLOWLOCATION option in case the document has a redirect on it.
If anyone is interested.. in the end, I end up using this code:
const char* f = "new_file.xml";
if (curl){
const char* c_url = "some_url";
FILE* ofile = fopen(f, "wb");
if (!ofile) { fprintf(stderr, "Failed to open file: %s\n", strerror(errno)); }
if (ofile){
curl_easy_setopt(curl, CURLOPT_URL, c_url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, ofile);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_perform(curl);
fclose(ofile);
}
}
pugi::xml_document doc;
doc.load_file(f);
Thanks for all the help guys!