0

I am attempting to retrieve the value of a single node in an XML document. enter image description here

Here are a couple of methods I have tried which don't work:

public class Location
{
    private String latitude;

    public void updateLocation(String woeid)
    {
        String query = String.Format("http://weather.yahooapis.com/forecastrss?w={0}", woeid);

        XmlDocument weatherData = new XmlDocument();
        weatherData.Load(query);

        XmlNode channel = weatherData.SelectSingleNode("rss").SelectSingleNode("channel");
        XmlNamespaceManager man = new XmlNamespaceManager(weatherData.NameTable);
        man.AddNamespace("geo:lat", "http://xml.weather.yahoo.com/ns/rss/1.0");
        latitude = channel.SelectSingleNode("geo:lat", man).InnerText;

    }

and this method, which also doesn't work:

public class Location
{
    private String latitude;

    public void updateLocation(String woeid)
    {
        String query = String.Format("http://weather.yahooapis.com/forecastrss?w={0}", woeid);

        XmlDocument weatherData = new XmlDocument();
        weatherData.Load(query);

        XmlNode channel = weatherData.SelectSingleNode("rss").SelectSingleNode("channel");
        latitude = channel.SelectSingleNode("item").SelectSingleNode("geo:lat").InnerText;

    }

}

Why am I not able to get the value of "geo:lat"?

crmepham
  • 4,676
  • 19
  • 80
  • 155
  • geo:lat is not the namespace, it is just "geo". lat is the element name. Have you looke at the SyndicationFeed class? https://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed.aspx – Crowcoder Jan 25 '15 at 16:22
  • Quite similar to your other questions, hm? – Jan Köhler Jan 25 '15 at 16:24

2 Answers2

0

1) The load method doesn't wait, so it doesn't have any content. See the HTTPGetXML() function to get the XML.

2) Check out how I'm creating the namespace, and using it.

3) Jump right to the node you want with //yweather:location.

Good Luck

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Net;
using System.IO;

namespace mblog {
    public partial class WebForm1 : System.Web.UI.Page {

        public XmlDocument HTTPGetXML(string strURL) {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strURL);
            req.Method = "GET";
            req.Timeout = 120000;  // 2 minutes

            XmlDocument xmlReturn = new XmlDocument();
            // UGH! not set by default!
            req.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

            // Set AllowWriteStreamBuffering to 'false'. 
            req.AllowWriteStreamBuffering = false;
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            string msg;

            try {
                // Get the response from the server
                StreamReader reader = new StreamReader(resp.GetResponseStream());
                msg = reader.ReadToEnd();
                xmlReturn.LoadXml(msg);
            } catch (Exception e) {
                throw new Exception("Error posting to server.", e);
            }
            return xmlReturn;
        }  

        protected void Page_Load(object sender, EventArgs e) {
            try {
                XmlDocument xmlWeather = HTTPGetXML("http://weather.yahooapis.com/forecastrss?w=2475688");

                NameTable nt = new NameTable();
                XmlNamespaceManager nsmgr;

                nsmgr = new XmlNamespaceManager(nt);
                nsmgr.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");


                XmlNode ndLocation = xmlWeather.SelectSingleNode("//yweather:location", nsmgr);
                if( ndLocation != null ) {
                    string strCity = ndLocation.Attributes["city"].Value;
                }


            } catch (Exception ex) {
                Response.Write( ex.Message );
            }


        }
    }
}
William Walseth
  • 2,803
  • 1
  • 23
  • 25
0

It turns out I was using the wrong namespace uri. There are two namespaces for the XML document one for yweather and a second for geo.

You will find the correct uri for the namespace you want to use at the top of the XML document.

This code lets me successfully retrieve the value of geo:lat:

public void updateLocation(String woeid)
        {
            String query = String.Format("http://weather.yahooapis.com/forecastrss?w={0}", woeid);

            XmlDocument weatherData = new XmlDocument();

            weatherData.Load(query);

            XmlNode channel = weatherData.SelectSingleNode("rss").SelectSingleNode("channel");
            XmlNamespaceManager man = new XmlNamespaceManager(weatherData.NameTable);
            man.AddNamespace("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#");

            latitude = channel.SelectSingleNode("item").SelectSingleNode("geo:lat", man).InnerText;

        }
crmepham
  • 4,676
  • 19
  • 80
  • 155