2015-01-25 3 views
0

XML 문서에서 단일 노드의 값을 검색하려고합니다. 또한 어떤 작동하지 않는XML에서 단일 노드를 선택하는 방법은 무엇입니까?

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; 

    } 

이 방법을 : 여기 enter image description here

가 작동하지 않습니다 내가 시도 방법의 커플

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; 

    } 

} 

이유를 모르겠습니다 "geo : lat"의 가치를 얻을 수 있습니까?

+0

지리 :

이 코드는 나를 성공적으로 geo:lat의 값을 검색 할 수 있습니다 네임 스페이스 아니다 위도를, 그냥 "지리적"입니다. lat는 요소 이름입니다. SyndicationFeed 수업을 들으셨습니까? https://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed.aspx – Crowcoder

+0

다른 질문과 매우 유사합니까, hm? – khlr

답변

0

1)로드 방법은 대기하지 않으므로 내용이 없습니다. XML을 얻으려면 HTTPGetXML() 함수를 참조하십시오.

2) 네임 스페이스를 만들고 사용하는 방법을 확인하십시오.

3) // yweather : location을 사용하여 원하는 노드로 바로 이동하십시오.

행운은

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); 
      } 


     } 
    } 
} 
0

내가 잘못된 네임 스페이스 URI를 사용하고 있었다 밝혀졌습니다. XML. 서의 이름 공간은 yweather이고 두 번째 이름은 geo입니다.

XML 문서 맨 위에서 사용할 네임 스페이스에 대한 올바른 URI를 찾을 수 있습니다.

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; 

     } 
관련 문제