View difference between Paste ID: 7qtJFV8q and pFs9S7a9
SHOW: | | - or go back to the newest paste.
1
using System;
2
using System.Xml;
3
using System.Net;
4
using System.IO;
5
6
// GetTemp 0.1
7
// Copyright (c) 2011 by Andrew Mock. All rights reserved.
8
// Special thanks to: marc_s on Stack Overflow
9
// 
10
// Description: Returns the current temp (F). You'll need a WeatherBug API key,
11
// and a WeatherBug Station ID.
12
// 
13-
            doc.LoadXml(downloadWebPage("http://www.andrewmock.com/uploads/9/1/0/7/9107466/example.xml"));
13+
// Syntax: GetTemp.exe APIKey stationID
14
// 
15-
            XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);
15+
//  calling it like this: GetTenp.exe A0000000000 STATN
16-
            man.AddNamespace("aws", "www.aws.com/aws");
16+
//  returns like this: 99.9
17
18-
            XmlNode weather = doc.SelectSingleNode("aws:weather", man);
18+
19-
            Console.WriteLine(weather.InnerText);
19+
20-
            Console.ReadKey(false);
20+
21
    {
22
        static void Main(string[] args)
23-
        private static string downloadWebPage(string url)
23+
24
            // new 'XML Document' object
25-
            // open a connection
25+
26-
            HttpWebRequest wReqObj = (HttpWebRequest)HttpWebRequest.Create(url);
26+
27
            // put the live weather "page" in the "blank" XML Document
28-
            // request response
28+
            doc.Load("http://www.andrewmock.com/uploads/9/1/0/7/9107466/example.xml");
29-
            WebResponse wResp = wReqObj.GetResponse();
29+
30
            // new 'XML Namespace Manager' object per our doc's NameTable
31-
            // open data stream
31+
            XmlNamespaceManager xmlnsm = new XmlNamespaceManager(doc.NameTable);
32-
            Stream wStream = wResp.GetResponseStream();
32+
33
            // add the "aws" XML Namespace (prefix) to the 'XML Namespace Manager'
34-
            // Create reader object:
34+
            xmlnsm.AddNamespace("aws", "http://www.aws.com/aws");
35-
            StreamReader sReader = new StreamReader(wStream);
35+
36
            // create the 'XML Node' object called weather for <aws:weather>
37-
            // read the entire stream content
37+
            XmlNode weather = doc.SelectSingleNode("aws:weather", xmlnsm);
38-
            string pageContent = sReader.ReadToEnd();
38+
39
            // create the 'XML Node' object called weather for <aws:temp>
40-
            // cleanup
40+
            XmlNode temp = weather.SelectSingleNode("aws:temp", xmlnsm);
41-
            sReader.Close();
41+
42-
            wStream.Close();
42+
            // write the temp in console
43-
            wResp.Close();
43+
            Console.WriteLine(temp.InnerText);
44
45-
            // return the actual web page as string
45+
            //Console.ReadKey(false);
46-
            return pageContent;
46+
47
    }
48
}
49
50