how to read the xml node attributes?

匿名 (未验证) 提交于 2019-12-03 00:44:02

问题:

I want to get the attributes values in xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>         <rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">             <channel>  <title>Yahoo! Weather - Chennai, IN</title> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Chennai__IN/*http://weather.yahoo.com/forecast/INXX0075_f.html</link> <description>Yahoo! Weather for Chennai, IN</description> <language>en-us</language> <lastBuildDate>Tue, 11 Dec 2012 10:10 am IST</lastBuildDate> <ttl>60</ttl> <yweather:location city="Chennai" region="TN"   country="India"/> <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/> <yweather:wind chill="82"   direction="90"   speed="5" /> <yweather:atmosphere humidity="74"  visibility="2.8"  pressure="29.94"  rising="0" /> <yweather:astronomy sunrise="6:22 am"   sunset="5:45 pm"/> <image> <title>Yahoo! Weather</title> <width>142</width> <height>18</height> <link>http://weather.yahoo.com</link> <url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url> </image> <item> <title>Conditions for Chennai, IN at 10:10 am IST</title> <geo:lat>13.1</geo:lat> <geo:long>80.29</geo:long> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Chennai__IN/*http://weather.yahoo.com/forecast/INXX0075_f.html</link> <pubDate>Tue, 11 Dec 2012 10:10 am IST</pubDate> <yweather:condition  text="Haze"  code="21"  temp="82"  date="Tue, 11 Dec 2012 10:10 am IST" /> <description><![CDATA[ <img src="http://l.yimg.com/a/i/us/we/52/21.gif"/><br /> <b>Current Conditions:</b><br /> Haze, 82 F<BR /> <BR /><b>Forecast:</b><BR /> Tue - Mostly Sunny. High: 86 Low: 70<br /> Wed - Mostly Sunny. High: 86 Low: 70<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Chennai__IN/*http://weather.yahoo.com/forecast/INXX0075_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/> (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/> ]]></description> <yweather:forecast day="Tue" date="11 Dec 2012" low="70" high="86" text="Mostly Sunny" code="34" /> <yweather:forecast day="Wed" date="12 Dec 2012" low="70" high="86" text="Mostly Sunny" code="34" /> <guid isPermaLink="false">INXX0075_2012_12_12_7_00_IST</guid> </item> </channel> </rss>  <!-- api9.weather.ch1.yahoo.com Tue Dec 11 05:27:35 PST 2012 --> 

I used the following code:

XmlDocument RSSXml = new XmlDocument();         RSSXml.Load("http://weather.yahooapis.com/forecastrss?w=29223178&u=f");         XmlNodeList RSSNodeList = RSSXml.SelectNodes("rss/channel");                    foreach (XmlNode RSSNode in RSSNodeList)         {             XmlNode RSSSubNode;             RSSSubNode = RSSNode.SelectSingleNode("title");             string title = RSSSubNode != null ? RSSSubNode.InnerText : "";             RSSSubNode = RSSNode.SelectSingleNode("link");             string link = RSSSubNode != null ? RSSSubNode.InnerText : "";             RSSSubNode = RSSNode.SelectSingleNode("description");             string desc = RSSSubNode != null ? RSSSubNode.InnerText : "";              lbl_title.Text = title;             lbl_link.Text = link;             lbl_desc.Text = desc;          }  

Its working well, but I want to get the yweather:location node attributes values. can you please help in this.

回答1:

Just get that node and use HasAttribute/GetAttribute methods



回答2:

You should provide a namespace to retrieve "yweather:location" node, and then use indexer to get attributes:

RSSSubNode = RSSNode["location", "http://xml.weather.yahoo.com/ns/rss/1.0"];  Console.WriteLine(RSSSubNode.Attributes["city"].Value); Console.WriteLine(RSSSubNode.Attributes["region"].Value); Console.WriteLine(RSSSubNode.Attributes["country"].Value); 

If you still want to use SelectSingleNode with XPath query, you should provide namespace manager to perform query with namespace prefixes:

var namespaceManager = new XmlNamespaceManager(new NameTable()); namespaceManager.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");  RSSSubNode = RSSNode.SelectSingleNode("yweather:location", namespaceManager); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!