如何读取xml节点属性?

时间:2012-12-11 05:07:37

标签: c# xml

我想获取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 -->

我使用了以下代码:

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;

        } 

它运行良好,但我想得到yweather:location节点属性值。能帮到你吗?

2 个答案:

答案 0 :(得分:0)

您应该提供一个命名空间来检索“yweather:location”节点,然后使用索引器来获取属性:

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

如果您仍希望将SelectSingleNode与XPath查询一起使用,则应提供命名空间管理器以使用命名空间前缀执行查询:

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

RSSSubNode = RSSNode.SelectSingleNode("yweather:location", namespaceManager);

答案 1 :(得分:0)

只需获取该节点并使用HasAttribute/GetAttribute methods