Some sort of weather script?http://forums.asp.net/t/919676.aspx/1?Some+sort+of+weather+script+Fri, 11 Nov 2005 07:44:03 -05009196761052435http://forums.asp.net/p/919676/1052435.aspx/1?Some+sort+of+weather+script+Some sort of weather script? <p>Hi all, I'm hoping someone can help me with this as i'm a true newbie at all of this stuff.</p> <p>What i'm wanting to do is get the current temperature for my home town and make it save into a text file and keep it updated every 30 mins or so.</p> <p>I've found this feed: <a href="http://www.wunderground.com/auto/rss_full/global/stations/94821.xml"> http://www.wunderground.com/auto/rss_full/global/stations/94821.xml</a><br> <br> Any help on what I do next?<br> <br> Please keep in mind i'm a newbie.<br> <br> Thanks for your time,<br> Scott</p> 2005-09-15T01:25:10-04:001054912http://forums.asp.net/p/919676/1054912.aspx/1?Re+Some+sort+of+weather+script+Re: Some sort of weather script? <font face="Verdana" size="2">1. Get the XML document into a System.Xml.XmlDocument.<br> 2. Select the node containing the text description (the site's RSS doesn't use namespaces, so it is easier to do this)<br> 3. Parse the temperature out of that node's inner text.<br> </font><br> <br> <p><font size="2"><font face="Courier New">System.Xml.XmlDocument xml = </font><font face="Courier New" color="#0000ff">new</font></font><font face="Courier New"><font size="2"> System.Xml.XmlDocument();<br> <br> xml.Load(&quot;http://www.wunderground.com/auto/rss_full/global/stations/94821.xml&quot;);<br> <br> System.Xml.XmlNode weather = xml.SelectSingleNode(&quot;/rss/channel/item/description&quot;);<br> <br> <font color="#0000ff">string</font> tempF = <font color="#0000ff">null</font></font></font><font face="Courier New"><font size="2">;<br> <font color="#0000ff">string</font> tempC = <font color="#0000ff">null</font></font></font><font face="Courier New"><font size="2">;<br> <br> <font color="#0000ff">if</font> (weather != <font color="#0000ff">null</font></font></font><font face="Courier New"><font size="2">)<br> {<br> &nbsp;&nbsp;&nbsp; <font color="#0000ff">string</font></font></font><font face="Courier New" size="2">[] tokens = weather.InnerText.Split(&quot; &quot;.ToCharArray());<br> &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; tempF = tokens[1];<br> &nbsp;&nbsp;&nbsp; tempC = tokens[3];<br> }<br> <br> Console.WriteLine(&quot;Temperature F: &quot; &#43; tempF);<br> Console.WriteLine(&quot;Temperature C: &quot; &#43; tempC);</font></p> 2005-09-17T02:47:40-04:001066372http://forums.asp.net/p/919676/1066372.aspx/1?Re+Some+sort+of+weather+script+Re: Some sort of weather script? <p>Sorry i got stuck on step 1.</p> <p>&nbsp;</p> <p>Also i think now we want all the information from the feed. notjust the current temperature.<br> <br> So basically I want to save that entire feed to a text file somehow and keep it updated every 15 minutes.<br> <br> I've googled for many hours trying to do this, so if anyone knows of any programs that can do it for me I'd be happy to know about them.<br> <br> Thanks for your time.<br> Scott</p> 2005-09-29T00:30:49-04:001066797http://forums.asp.net/p/919676/1066797.aspx/1?Re+Some+sort+of+weather+script+Re: Some sort of weather script? <p><font face="Courier New"><font size="2"><font face="Verdana">Let me try adding some more descriptions for you.<br> <br> 1. Get the XML document into a System.Xml.XmlDocument.&nbsp; <br> <br> XmlDocument is a way of loading the XML tree into memory as a .NET object, which you can then navigate through using code.<br> </font><br> <br> <br> System.Xml.XmlDocument xml = <font color="#0000ff">new</font></font></font><font face="Courier New"><font size="2"> System.Xml.XmlDocument();<br> xml.Load(&quot;http://www.wunderground.com/auto/rss_full/global/stations/94821.xml&quot;);<br> <br> <br> <br> <font face="Verdana">2. Select the node containing the text description (the site's RSS doesn't use namespaces, so it is easier to do this).&nbsp; <br> <br> In the XML, the description text is contained in the &lt;description&gt; node, which is in a &lt;item&gt; node, which is in a &lt;channel&gt; node, which is in the top &lt;rss&gt; node.&nbsp; <br> <br> Just like a directory path, the XPath to your &lt;description&gt; node looks like: /rss/channel/item/description<br> </font><br> <font face="Verdana">If we use the SelectSingleNode() method, then we will be left with a reference to the node within the XmlDocument.<br> </font><br> <br> <br> System.Xml.XmlNode weather = xml.SelectSingleNode(&quot;/rss/channel/item/description&quot;);<br> <br> <br> </font></font><font face="Courier New"><font size="2"><font color="#0000ff"><br> <font face="Verdana"><font color="#000000">3. Get the inner text of this node, and you have your full weather description from the RSS feed.<br> <br> In the following, I declare a string to hold the weather description, and initialize it to &quot;(no data)&quot;.&nbsp; This way, if the node was not found in step 2, then the string will still have some semi-meaningful data<br> </font></font><br> <br> string<font face="Courier New" color="#000000" size="2"> description = &quot;(no data)&quot;;<br> </font><br> if</font> (weather != <font color="#0000ff">null</font></font></font><font face="Courier New"><font size="2">)<br> {<br> &nbsp;&nbsp;&nbsp; </font></font><font face="Courier New" size="2">description = weather.InnerText;<br> }<br> <br> <br> <br> <font face="Verdana">4. To save this as a file, do something like the following (untested):<br> </font><br> System.IO.StreamWriter sw = new System.IO.StreamWriter(@&quot;c:\weather.txt&quot;, false);<br> sw.WriteLine(description);<br> sw.Close();<br> sw&nbsp;= null;<br> <br> <font face="Verdana">What this does is creates a new streamwriter using a filename and no appending (meaning that it will overwrite the file if it already exists).&nbsp; Then it writes the descriptions string to the file.&nbsp; Then it closes the file.<br> <br> In the end, c:\weather.txt will contain only the description text from your RSS feed.</font></font></p> 2005-09-29T11:20:58-04:001067648http://forums.asp.net/p/919676/1067648.aspx/1?Re+Some+sort+of+weather+script+Re: Some sort of weather script? Thanks for the information, just curious will this text file be automatically updated every say 15 - 30 mins with the new current temperature?<br> <br> Also is all this written in Visual studio .NET ? 2005-09-30T00:37:33-04:001067686http://forums.asp.net/p/919676/1067686.aspx/1?Re+Some+sort+of+weather+script+Re: Some sort of weather script? <p>This is C# code, which is one of the languages of .NET.&nbsp; You would typically use an IDE like Visual Studio.NET to create programs, but it certainly is not required.&nbsp; Everything that you need to compile programs is actually part of the .NET Framework itself.&nbsp; There are even other free IDE's out there (SharpDevelop comes to mind), and Visual Studio 2005 will have a free Express version for the hobbiest developer to use (you should be able to find a beta version today, and a final version in November 2005).<br> <br> This code is only the functional fragment of what a larger application would be.&nbsp; You could take this code, drop it into the main() of a console application (think&nbsp;DOS program), and then use Task Scheduler to run the console app every 15 minutes.&nbsp; That would overwrite the file every time that the task runs.<br> <br> </p> 2005-09-30T01:27:03-04:001071980http://forums.asp.net/p/919676/1071980.aspx/1?Re+Some+sort+of+weather+script+Re: Some sort of weather script? Hi mate,<br> <br> So close to having it done, but I'm getting an error on this part &quot;<font face="Courier New" size="2">sw.WriteLine(<strong><em>description</em></strong>);&quot;<br> <br> I'm not on the computer with VS .NET at the moemnt so can't tell you the exact error message, any ideas? Will you need to exact error message?<br> <br> Thanks for your help.<br> Scott</font> 2005-10-05T00:44:21-04:001073392http://forums.asp.net/p/919676/1073392.aspx/1?Re+Some+sort+of+weather+script+Re: Some sort of weather script? Nevermind, I worked it out.<br> <br> Thanks mate!<br> Scott 2005-10-06T04:26:24-04:001073668http://forums.asp.net/p/919676/1073668.aspx/1?Re+Some+sort+of+weather+script+Re: Some sort of weather script? Cheers! 2005-10-06T11:32:00-04:001079097http://forums.asp.net/p/919676/1079097.aspx/1?Re+Some+sort+of+weather+script+Re: Some sort of weather script? <p>Hey mate,</p> <p>Having some troubles now, 1st when the file saves as weather.txt when i open it up in notepad it will read this as an example: 10°C , Looks great doesn't it? But when i try open it up in Word Pad it looks like this: <font size="2">10°C<br> <br> Which is no good, any ideas on how to fix this?<br> <br> And the other problem i'm having is try to save that entire text in Description, so i get the current temperature, wind direction etc. Any ideas how i get it all?<br> <br> Thanks,<br> Scott</p> </font> 2005-10-11T10:41:06-04:001083893http://forums.asp.net/p/919676/1083893.aspx/1?Re+Some+sort+of+weather+script+Re: Some sort of weather script? <font face="Verdana" size="2">The first code that I posted tokenized the description so that you could pull out the temperatures.&nbsp; However, the second code that I posted (with descriptions) grabbed the entire description for you, since you mentioned that you were interested in that.&nbsp; <br> <br> This is the code snippet that returns the entire description:<br> </font><br> <font face="Courier New" color="#0000ff" size="2">string</font><font face="Courier New" color="#000000" size="2"> description = &quot;(no data)&quot;;<br> </font><br> <font face="Courier New"><font size="2"><font color="#0000ff">if</font> (weather != <font color="#0000ff">null</font></font></font><font face="Courier New" size="2">)<br> {<br> &nbsp;&nbsp;&nbsp; </font><font face="Courier New" size="2">description = weather.InnerText;<br> }<br> <br> <br> <br> <font face="Times New Roman" size="3"><font face="Verdana" size="2">Now, for the</font>: </font>10°C<br> <br> <font face="Times New Roman" size="3"><font face="Verdana" size="2">Short answer: you need to specify an encoding in the constructor of the StreamWriter:<br> </font><br> <font face="Courier New" size="2">System.IO.StreamWriter sw = <br> &nbsp;&nbsp; new System.IO.StreamWriter(@&quot;c:\weather.txt&quot;, false, System.Text.Encoding.UTF8);</font><br> <br> <br> <font face="Verdana" size="2">Long answer:<br> <br> The degree character is Unicode hex 0x00B0 and ASCII hex 0xA1.&nbsp; UTF-8 is an in-between encoding that will represent the lower 127 ASCII characters as one byte, and other Unicode characters using multiple bytes.<br> <br> Since the ASCII value is greater than 0x80, when represented as UTF-8, it must be represented as the encoded Unicode value.&nbsp; So, within a UTF-8 stream, the Unicode character 0x00B0 looks like 0xC2B0.&nbsp; Weird, right?<br> <br> Well, the decoding of these two bytes works like this:<br> <br> 1. 0xC2 is greater than 0x80, so we know that this is not just an ASCII character.&nbsp; Start decoding.<br> 2. 0xC2 is between 0xC0 and 0xDF, so there is only two encoded bytes.&nbsp; Work on 0xC2 0xB0 only for the next character.<br> 3. Subtract 0xC0 from first byte, then multiply result by 0x40:&nbsp;&nbsp; 0xC2 - 0xC0 = 2 * 0x40 = 0x80<br> 4. Subtract 0x80 from second byte: 0xB0 - 0x80 = 0x30<br> 5. Add the results from 3 and 4 together, represent result using 16-bits: 0x80 &#43; 0x30 = 0x00B0<br> <br> <br> When WordPad opened the file, it didn't detect any Encoding header bytes at the start of the file, so it interpreted everything as <strike>ASCII</strike> ANSI (Notepad obviously doesn't do this).&nbsp; This means that each byte was represented as a single character.&nbsp; 0xC2 is the Latin A with circumflex, 0xB0 is the degree symbol.<br> <br> However, by specifying the Encoding in the StreamWriter constructor, there are several bytes prepended to your file.&nbsp; WordPad will detect these bytes, and then properly decode the UTF-8 characters.<br> <br> Reference:<br> </font><a href="http://www1.tip.nl/~t876506/utf8tbl.html"><font face="Verdana" size="2">http://www1.tip.nl/~t876506/utf8tbl.html</font></a><br> </font></font> 2005-10-15T13:19:14-04:001088370http://forums.asp.net/p/919676/1088370.aspx/1?Re+Some+sort+of+weather+script+Re: Some sort of weather script? thanks for this, i'll give it ago very soon!<br> <br> i'll post back and let you know if it works. 2005-10-19T22:53:29-04:001102084http://forums.asp.net/p/919676/1102084.aspx/1?Re+Some+sort+of+weather+script+Re: Some sort of weather script? Hey mate sorry for the delay in replying. However it's still not working, well it now works fine in Wordpad but not in the program we are wanting it to work in.<br> <br> Is there any hope left?<br> <br> Thanks,<br> Scott 2005-11-03T21:51:02-05:001102215http://forums.asp.net/p/919676/1102215.aspx/1?Re+Some+sort+of+weather+script+Re: Some sort of weather script? Well I worked it out, changed the encoding to Default and now it works fine!<br> <br> Thanks for all your help! 2005-11-04T00:48:44-05:001102365http://forums.asp.net/p/919676/1102365.aspx/1?Re+Some+sort+of+weather+script+Re: Some sort of weather script? <blockquote><span class="icon-blockquote"></span> <h4>JasonFollas</h4> <font size="2"><font face="Courier New"><font color="#0000ff">string</font><font size="2">[] tokens = weather.InnerText.Split(&quot; &quot;.ToCharArray());</font></font></font></blockquote> <br> <br> <font size="2">Wouldn't splitting like this weed out some junk tokens?<br> </font><br> <font face="Courier New"><font size="1"><font color="#0000ff">string</font>[] tokens = weather.InnerText.Split(<font color="#a52a2a"><strong>&quot; |&nbsp;&quot;</strong></font>.ToCharArray());</font></font> 2005-11-04T06:37:25-05:001109547http://forums.asp.net/p/919676/1109547.aspx/1?Re+Some+sort+of+weather+script+Re: Some sort of weather script? <p>The way that split works is that you give it a list (array)&nbsp;of single characters, and every time that it finds a character in that list, it starts a new element in a string array (that is returned).&nbsp; So, your split definition is really only tokenizing when it comes to a space or a pipe, even though you provide two spaces.&nbsp; It won't treat space-pipe-space as a single split point.<br> <br> For reference, here's the &lt;description&gt; element from&nbsp;that XML feed:<br> <br> <font face="Courier New"><font size="2"><span class="m"><font color="#0000ff">&lt;</font></span><span class="t"><font color="#990000">description</font></span><span class="m"><font color="#0000ff">&gt;</font></span><span class="tx"><strong>Temperature: 60°F / 16°C | Humidity: 39% | Pressure: 30.07in / 1018hPa | Conditions: | Wind Direction: West | Wind Speed: 9mph / 15km/h | Updated: 10:30 AM CST</strong></span><span class="m"><font color="#0000ff">&lt;/</font></span><span class="t"><font color="#990000">description</font></span><span class="m"><font color="#0000ff">&gt;</font></span> <br> </font></font><br> So, if you want access to each subcomponent individually, then you could split on the pipe alone (&quot;|&quot;) in order to separate the data into the various components:<br> <br> <span class="tx"><strong><font face="Courier New" size="2">{0}=&quot;Temperature: 60°F / 16°C&nbsp;&quot;<br> {1}=&quot; Humidity: 39%&nbsp;&quot;<br> {2}=&quot;&nbsp;Pressure: 30.07in / 1018hPa&nbsp;&quot;<br> {3}=&quot;&nbsp;Conditions:&nbsp;&quot;<br> {4}=&quot;&nbsp;Wind Direction: West&nbsp;&quot;<br> {5}=&quot;&nbsp;Wind Speed: 9mph / 15km/h&nbsp;&quot;<br> {6}=&quot;&nbsp;Updated: 10:30 AM CST&quot;<br> </font></strong><br> Then, you can trim each (if you need to get rid of leading/trailing spaces), and further split based on space (or whatever you want to do to parse each element).&nbsp; I don't have an elegant way to do it all in one operation, other than splitting the entire description text on the space character.</span></p> 2005-11-11T00:39:05-05:001109776http://forums.asp.net/p/919676/1109776.aspx/1?Re+Some+sort+of+weather+script+Re: Some sort of weather script? Thank you. Good point 2005-11-11T07:44:03-05:00