1. Get the XML document into a System.Xml.XmlDocument.
2. Select the node containing the text description (the site's RSS doesn't use namespaces, so it is easier to do this)
3. Parse the temperature out of that node's inner text.
System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
1. Get the XML document into a System.Xml.XmlDocument.
XmlDocument is a way of loading the XML tree into memory as a .NET object, which you can then navigate through using code.
System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
xml.Load("http://www.wunderground.com/auto/rss_full/global/stations/94821.xml");
2. Select the node containing the text description (the site's RSS doesn't use namespaces, so it is easier to do this).
In the XML, the description text is contained in the <description> node, which is in a <item> node, which is in a <channel> node, which is in the top <rss> node.
Just like a directory path, the XPath to your <description> node looks like: /rss/channel/item/description
If we use the SelectSingleNode() method, then we will be left with a reference to the node within the XmlDocument.
3. Get the inner text of this node, and you have your full weather description from the RSS feed.
In the following, I declare a string to hold the weather description, and initialize it to "(no data)". This way, if the node was not found in step 2, then the string will still have some semi-meaningful data
string description = "(no data)";
if (weather != null)
{
description = weather.InnerText;
}
4. To save this as a file, do something like the following (untested):
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). Then it writes the descriptions string to the file. Then it closes the file.
In the end, c:\weather.txt will contain only the description text from your RSS feed.
This is C# code, which is one of the languages of .NET. You would typically use an IDE like Visual Studio.NET to create programs, but it certainly is not required. Everything that you need to compile programs is actually part of the .NET Framework itself.
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).
This code is only the functional fragment of what a larger application would be. You could take this code, drop it into the main() of a console application (think DOS program), and then use Task Scheduler to run the console app every 15 minutes. That would
overwrite the file every time that the task runs.
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:
10°C
Which is no good, any ideas on how to fix this?
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?
Nibbleman
Member
45 Points
9 Posts
Some sort of weather script?
Sep 15, 2005 01:25 AM|LINK
Hi all, I'm hoping someone can help me with this as i'm a true newbie at all of this stuff.
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.
I've found this feed: http://www.wunderground.com/auto/rss_full/global/stations/94821.xml
Any help on what I do next?
Please keep in mind i'm a newbie.
Thanks for your time,
Scott
JasonFollas
Participant
860 Points
170 Posts
Re: Some sort of weather script?
Sep 17, 2005 02:47 AM|LINK
2. Select the node containing the text description (the site's RSS doesn't use namespaces, so it is easier to do this)
3. Parse the temperature out of that node's inner text.
System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
xml.Load("http://www.wunderground.com/auto/rss_full/global/stations/94821.xml");
System.Xml.XmlNode weather = xml.SelectSingleNode("/rss/channel/item/description");
string tempF = null;
string tempC = null;
if (weather != null)
{
string[] tokens = weather.InnerText.Split(" ".ToCharArray());
tempF = tokens[1];
tempC = tokens[3];
}
Console.WriteLine("Temperature F: " + tempF);
Console.WriteLine("Temperature C: " + tempC);
A View Inside My Head
Nibbleman
Member
45 Points
9 Posts
Re: Some sort of weather script?
Sep 29, 2005 12:30 AM|LINK
Sorry i got stuck on step 1.
Also i think now we want all the information from the feed. notjust the current temperature.
So basically I want to save that entire feed to a text file somehow and keep it updated every 15 minutes.
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.
Thanks for your time.
Scott
JasonFollas
Participant
860 Points
170 Posts
Re: Some sort of weather script?
Sep 29, 2005 11:20 AM|LINK
Let me try adding some more descriptions for you.
1. Get the XML document into a System.Xml.XmlDocument.
XmlDocument is a way of loading the XML tree into memory as a .NET object, which you can then navigate through using code.
System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
xml.Load("http://www.wunderground.com/auto/rss_full/global/stations/94821.xml");
2. Select the node containing the text description (the site's RSS doesn't use namespaces, so it is easier to do this).
In the XML, the description text is contained in the <description> node, which is in a <item> node, which is in a <channel> node, which is in the top <rss> node.
Just like a directory path, the XPath to your <description> node looks like: /rss/channel/item/description
If we use the SelectSingleNode() method, then we will be left with a reference to the node within the XmlDocument.
System.Xml.XmlNode weather = xml.SelectSingleNode("/rss/channel/item/description");
3. Get the inner text of this node, and you have your full weather description from the RSS feed.
In the following, I declare a string to hold the weather description, and initialize it to "(no data)". This way, if the node was not found in step 2, then the string will still have some semi-meaningful data
string description = "(no data)";
if (weather != null)
{
description = weather.InnerText;
}
4. To save this as a file, do something like the following (untested):
System.IO.StreamWriter sw = new System.IO.StreamWriter(@"c:\weather.txt", false);
sw.WriteLine(description);
sw.Close();
sw = null;
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). Then it writes the descriptions string to the file. Then it closes the file.
In the end, c:\weather.txt will contain only the description text from your RSS feed.
A View Inside My Head
Nibbleman
Member
45 Points
9 Posts
Re: Some sort of weather script?
Sep 30, 2005 12:37 AM|LINK
Also is all this written in Visual studio .NET ?
JasonFollas
Participant
860 Points
170 Posts
Re: Some sort of weather script?
Sep 30, 2005 01:27 AM|LINK
This is C# code, which is one of the languages of .NET. You would typically use an IDE like Visual Studio.NET to create programs, but it certainly is not required. Everything that you need to compile programs is actually part of the .NET Framework itself. 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).
This code is only the functional fragment of what a larger application would be. You could take this code, drop it into the main() of a console application (think DOS program), and then use Task Scheduler to run the console app every 15 minutes. That would overwrite the file every time that the task runs.
A View Inside My Head
Nibbleman
Member
45 Points
9 Posts
Re: Some sort of weather script?
Oct 05, 2005 12:44 AM|LINK
So close to having it done, but I'm getting an error on this part "sw.WriteLine(description);"
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?
Thanks for your help.
Scott
Nibbleman
Member
45 Points
9 Posts
Re: Some sort of weather script?
Oct 06, 2005 04:26 AM|LINK
Thanks mate!
Scott
JasonFollas
Participant
860 Points
170 Posts
Re: Some sort of weather script?
Oct 06, 2005 11:32 AM|LINK
A View Inside My Head
Nibbleman
Member
45 Points
9 Posts
Re: Some sort of weather script?
Oct 11, 2005 10:41 AM|LINK
Hey mate,
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: 10°C
Which is no good, any ideas on how to fix this?
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?
Thanks,
Scott