C# 雅虎天气Rss阅读与ASP.NETMVC?
我想在这里解析yahoo weather rss提要xml: 我的Rss项目C# 雅虎天气Rss阅读与ASP.NETMVC?,c#,rss,C#,Rss,我想在这里解析yahoo weather rss提要xml: 我的Rss项目 public class YahooWeatherRssItem { public string Title { get; set; } public string Link { get; set; } public string Description { get; set; } public string City { get; set; } public string Co
public class YahooWeatherRssItem
{
public string Title { get; set; }
public string Link { get; set; }
public string Description { get; set; }
public string City { get; set; }
public string Country { get; set; }
// temp, wind, etc...
}
我的Rss管理器
public static IEnumerable<YahooWeatherRssItem> GetYahooWeatherRssItems(string rssUrl)
{
XDocument rssXml = XDocument.Load(rssUrl);
var feeds = from feed in rssXml.Descendants("item")
select new YahooWeatherRssItem
{
I can get following values
Title = feed.Element("title").Value,
Link = feed.Element("link").Value,
Description = feed.Element("description").Value,
// I dont know, How can I parse these.
Text = ?
Temp = ?
Code = ?
};
return feeds;
}
公共静态IEnumerable GetyahooWeathersSitems(字符串rssUrl)
{
XDocument rssXml=XDocument.Load(rssUrl);
var feed=来自rssXml.subjects(“项”)中的提要
选择新YahooWeathersSitem
{
我可以得到以下值
Title=feed.Element(“Title”).Value,
Link=feed.Element(“Link”).Value,
Description=feed.Element(“Description”).值,
//我不知道,我如何解析这些。
Text=?
温度=?
代码=?
};
回馈;
}
我不知道,如何解析以下xml行:
<yweather:condition text="Mostly Cloudy" code="28" temp="50" date="Fri, 18 Dec 2009 9:38 am PST" />
<yweather:location city="Sunnyvale" region="CA" country="United States"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="50" direction="0" speed="0" />
<yweather:atmosphere humidity="94" visibility="3" pressure="30.27" rising="1" />
<yweather:astronomy sunrise="7:17 am" sunset="4:52 pm"/>
问题是y天气:
。可能有一篇文章是关于类似这种结构的xml解析的。还是代码示例
谢谢。您需要读取这些值的XML属性。根据此处的问题(),您可以尝试以下方法:
Temp = feed.Attribute("temp");
下面的表达式应该可以工作,首先引用
ycweather
名称空间
XNamespace yWeatherNS = "http://xml.weather.yahoo.com/ns/rss/1.0";
然后通过以下方式获得属性值:
Text = feed.Element(yWeatherNS + "condition").Attribute("text").Value
问题是您的条件元素位于另一个命名空间中,因此必须通过在该命名空间的上下文中选择此节点
您可以通过MSDN文章使用名称空间并使用
属性获取数据
XNamespace ns = "http://xml.weather.yahoo.com/ns/rss/1.0";
var feeds = from feed in rssXml.Descendants("item")
select new YahooWeatherRssItem
{
Title = feed.Element("title").Value,
Link = feed.Element("link").Value,
Description = feed.Element("description").Value,
Code=feed.Element(ns+"condition").Attribute("code").Value
//like above line, you can get other items
};
这会奏效的已测试:)您需要访问属性的-请参阅以获取示例。例如,在您的实例中,
feed.Element(“condition”).Attribute(“text”).Value
当我写入string Value=xdoc.Root.Element(“yweather:condition”).Attribute(“text”).Value代码>我得到了这个错误:“十六进制值0x3A的“:”字符不能包含在名称中。”当我写入feed.Element(“condition”).Attribute(“text”).value时,字符串
我得到了空对象引用错误。我调试了它。我正在获取包含正确数据的xml文件。在如下属性之前有一个字符串: