如何读取xml节点属性? [英] how to read the xml node attributes?

查看:29
本文介绍了如何读取xml节点属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取 xml 中的属性值:

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

我使用了以下代码:

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 节点属性值.你能帮忙吗.

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

推荐答案

你应该提供一个命名空间来检索yweather:location"节点,然后使用 indexer 来获取属性:

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

如果你仍然想在 XPath 查询中使用 SelectSingleNode,你应该提供命名空间管理器来执行带有命名空间前缀的查询:

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

这篇关于如何读取xml节点属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆