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 Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 35541802 › c-sharp-code-to-read-xml-from-url
C# code to Read XML From URL - Stack Overflow
Closed 9 years ago. ... public void getJobs(){ XmlDocument doc = new XmlDocument(); string url = @"http://api.indeed.com/ads/apisearch?publisher=5566998848654317&v=2&q=java&filter=1&limit=25"; doc.Load(url); doc.Save(@"C:\indeed.xml"); }
🌐
Microsoft Learn
learn.microsoft.com › en-us › troubleshoot › developer › visualstudio › csharp › language-compilers › read-xml-data-from-url
Read XML data from a URL by using C# - C# | Microsoft Learn
April 4, 2023 - Describes how to use the XmlTextReader class to read XML from a URL by using Visual C#. Also provides a code sample that illustrates this task.
🌐
O'Reilly
oreilly.com › library › view › c-cookbook › 0596003390 › ch17s02.html
17.2. Reading XML on the Web - C# Cookbook [Book]
string url = "http://localhost/xml/sample.xml"; // use the XmlTextReader to get the xml at the url XmlTextReader reader = new XmlTextReader (url); while (reader.Read( )) { switch (reader.NodeType) { case XmlNodeType.Element : Console.Write("<{0}>", ...
🌐
Lazarus
forum.lazarus.freepascal.org › index.php
How to get XML from URL - Lazarus Forum - Free Pascal
How to get XML from URL · Free Pascal · Website · Downloads · Wiki · Documentation · Bugtracker · Mailing List · Lazarus · Website · Downloads (Laz+FPC) Packages (OPM) FAQ · Wiki · Documentation (RTL/FCL/LCL) Bugtracker · CCR Bugs · GIT · Mailing List ·
Top answer
1 of 1
1
[HttpPost] · public async Task GetxmlData(Input model) · { · //requesting the particular web page · ServicePointManager.Expect100Continue = true; · ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; · var httpRequest = (HttpWebRequest)WebRequest.Create(model.Url); · string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("bhnsmr0001" + ":" + "k@udQWw;b$9Ml*V")); · httpRequest.Headers.Add("Authorization", "Basic " + svcCredentials); · //geting the response from the request url · // Get response · var response = (HttpWebResponse)httpRequest.GetResponse(); · // create a stream to hold the contents of the response (in this case it is the contents of the XML file · var receiveStream = response.GetResponseStream(); · StreamReader rdr = new StreamReader(response.GetResponseStream()); ///// IT'S taking more time to read Large amount of Data · string strResponse = rdr.ReadToEnd(); · // creating XML document · var mySourceDoc = new XmlDocument(); · //load the file from the stream · mySourceDoc.LoadXml(strResponse); · //XmlNode nd = mySourceDoc.DocumentElement.SelectSingleNode("//records"); · XmlDocument xmlAPDP = new XmlDocument(); · XmlNodeReader xmlReader = new XmlNodeReader(mySourceDoc); · DataSet dataSet = new DataSet(); · dataSet.ReadXml(xmlReader); · rdr.Close(); · WriteXML(dataSet); · } · public static string WriteXML(DataSet ds) · string cs = ConfigurationManager.ConnectionStrings["CS"].ConnectionString; · using (SqlConnection con = new SqlConnection(cs)) · { · DataTable dtrecord = ds.Tables["record"]; · con.Open(); · using (SqlBulkCopy sb = new SqlBulkCopy(con)) · { · sb.DestinationTableName = "record"; · sb.ColumnMappings.Add("record_Id", "record_Id"); · sb.ColumnMappings.Add("category", "category"); · sb.ColumnMappings.Add("editor", "editor"); · sb.ColumnMappings.Add("entered", "entered"); · sb.ColumnMappings.Add("sub-category", "sub-category"); · sb.ColumnMappings.Add("uid", "uid"); · sb.ColumnMappings.Add("updated", "updated"); · sb.WriteToServer(dtrecord); · } · } · }
🌐
ReqBin
reqbin.com › req › c-eanbjsr1 › curl-get-xml-example
How do I get XML using Curl?
To retrieve an XML from the server using Curl, you need to pass the target URL to Curl along with the -H "Accept: application/xml" command line option. The -H command line switch sends an Accept: application/xml header to the server and tells ...
🌐
Stack Overflow
stackoverflow.com › questions › 22808200 › trying-to-get-xml-from-an-url
android - Trying to get XML from an URL - Stack Overflow
April 2, 2014 - It confused me couple of days. AssetManager asset = getAssets(); InputStream input = asset.open("student.xml"); List<Student> list = ParserByPULL.getStudents(input); Everything works fine if the file in assets folder. Then I tried to get it from an URL.
Find elsewhere
🌐
ReqBin
reqbin.com › req › csharp › z1miyucm › get-xml-example
C#/.NET | How do I get an XML from the server?
Without this header, the server may return data in a different format. The Content-Type: application/xml response header informs the client that the server returned XML. In this C#/.NET GET XML Example, we send a GET request to the ReqBin echo URL with Accept: application/xml request header.
🌐
ASPSnippets
aspsnippets.com › Articles › 3983 › Read-Parse-XML-from-URL-in-ASPNet-using-C-and-VBNet
Read Parse XML from URL in ASPNet using C and VBNet
August 19, 2023 - The XML will be first downloaded from an URL using WebClient class and then will be converted to DataSet. Finally, the DataSet will be used to populate the GridView control in ASP.Net using C# and VB.Net.
🌐
Unity
forum.unity.com › unity engine
[C# Scripting] - Read XML from URL Address - Unity Engine - Unity Discussions
June 28, 2017 - Hi. I need to read a XML File from my URL. I have a C# code, How could I alter my code to read that XML File from URL? My XML File <group> <user> <name>John</name> <addr>98 Kent Street</addr> <phone>0423…
🌐
Stack Overflow
stackoverflow.com › questions › 3498758 › extract-url-from-xml-response
c - extract url from xml response - Stack Overflow
You shouldn't. If you have the option, you should use an XML processor for any number or reasons. But if you must, you can do something like "rootContainer.xlink:href=\"([^\"]+)\" Syntax may vary depending on what regex library you're using - there isn't a single "regex" syntax. ... Sign up to request clarification or add additional context in comments. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Live from re:Invent…it’s Stack Overflow!
🌐
[Code Destination]
nishantwork.wordpress.com › 2012 › 10 › 10 › how-to-get-xml-data-from-url-in-c
How to get XML data from URL in c#. | [Code Destination]
October 10, 2012 - For retrieving value from url and save as xml in format can be done by c#. I have given below code that will take website url as input parameter and return in xml form data . Make Sure you have used: Used name space: using System.Net; /// <summary> /// Get Data in xml format by url /// </summary> /// <param name="url"></param> /// <returns></returns> public static XmlDocument GetXmlDataFromUrl(string url) { //requesting the particular web page var httpRequest = (HttpWebRequest)WebRequest.Create(url); //geting the response from the request url var response = (HttpWebResponse)httpRequest.GetResp
🌐
Safe Community
community.safe.com › home › forums › fme form › data › extract xml from url
Extract XML from URL - FME Community - Safe Software
April 29, 2020 - You can then use a ListExploder to get all the messages per logger: That's very helpful - thank you. I may be back with more questions tho (sorry in advance) ... You might want to e.g. use the StringReplacer to fix those first. You can then read the XML like this (or similar using the FeatureReader): ... Looks like I'm going to have to parameterize my api in order to fire a list of values into the url string.
Top answer
1 of 2
2

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.

2 of 2
0

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!

🌐
SitePoint
sitepoint.com › php
How to capture an XML file from URL? - PHP - SitePoint Forums | Web Development & Design Community
October 21, 2013 - I have FTP access to my own domain, ... itself, from where I can download them at my leisure. ... $showqty = 100; // change as required for $param = "retailers", "categories", "subcategories", "discounts"; $url = $start-of-url . $param . $end-of-url . $showqty; $xmldata = file_get_contents($url); ...