Quang Dinh Luong

Quang Dinh Luong

  • NA
  • 76
  • 18.8k

Error - XML NamespaceManager is needed!

Mar 25 2016 1:18 PM

I'm trying to parse Yahoo weather xml file (https://query.yahooapis.com/v1/public/yql?q=select+*+from+weather.forecast+where+woeid%3D91888417&format=xml) to my project.
But this error appears:  Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function. at the red line below!
This xml file is really weird cause it has 2 prefix (maybe).

So I try:
  1. string xmlWeatherAll = string.Format("https://query.yahooapis.com/v1/public/yql?q=select+*+from+weather.forecast+where+woeid%3D{0}&format=xml", woeid);
     
    XmlDocument xmlData = new XmlDocument();
    xmlData.Load(xmlWeatherAll);
     

    string xmlWeatherAll = string.Format("https://query.yahooapis.com/v1/public/yql?q=select+*+from+weather.forecast+where+woeid%3D{0}&format=xml", woeid);
    XmlNamespaceManager xmlManager = new XmlNamespaceManager(xmlData.NameTable);
    xmlManager.AddNamespace("yahoo", "http://www.yahooapis.com/v1/base.rng");
    XmlNodeList nodes = xmlData.SelectNodes("/query/results/channel/item/yahoo:forecast", xmlManager);
    XmlNamespaceManager xmlManager2 = new XmlNamespaceManager(xmlData.NameTable);
    xmlManager2.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
    XmlNode channel = xmlData.SelectSingleNode("query").SelectSingleNode("results").SelectSingleNode("channel", xmlManager2);
  2. title = channel.SelectSingleNode("title").InnerText;
    currentCondition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", xmlManager2).Attributes["text"].Value;
     

 But it still have the same error at the red (I colored) line.
I'm still newbie to this thing, please help me!
Thanks in advance!
I also upload the file, it's a library (very small). Maybe a glance at it maybe better to fix this problem!

 

Attachment: weathercheck.rar