How do I use XML prefixes in C#?
- by Andrew Mock
EDIT: I have now published my app: http://pastebin.com/PYAxaTHU
I was trying to make console-based application that returns my temperature.
using System;
using System.Xml;
namespace GetTemp
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(downloadWebPage(
"http://www.andrewmock.com/uploads/example.xml"
));
XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);
man.AddNamespace("aws", "www.aws.com/aws");
XmlNode weather = doc.SelectSingleNode("aws:weather", man);
Console.WriteLine(weather.InnerText);
Console.ReadKey(false);
}
}
}
Here is the sample XML:
<aws:weather xmlns:aws="http://www.aws.com/aws">
<aws:api version="2.0"/>
<aws:WebURL>http://weather.weatherbug.com/WA/Kenmore-weather.html?ZCode=Z5546&Units=0&stat=BOTHL</aws:WebURL>
<aws:InputLocationURL>http://weather.weatherbug.com/WA/Kenmore-weather.html?ZCode=Z5546&Units=0</aws:InputLocationURL>
<aws:station requestedID="BOTHL" id="BOTHL" name="Moorlands ES" city="Kenmore" state=" WA" zipcode="98028" country="USA" latitude="47.7383346557617" longitude="-122.230278015137"/>
<aws:current-condition icon="http://deskwx.weatherbug.com/images/Forecast/icons/cond024.gif">Mostly Cloudy</aws:current-condition>
<aws:temp units="°F">40.2</aws:temp>
<aws:rain-today units=""">0</aws:rain-today>
<aws:wind-speed units="mph">0</aws:wind-speed>
<aws:wind-direction>WNW</aws:wind-direction>
<aws:gust-speed units="mph">5</aws:gust-speed>
<aws:gust-direction>NW</aws:gust-direction>
</aws:weather>
I'm just not sure how to use XML prefixes correctly here. What is wrong with this?