C# 解析RSS中的信息
我手头有一项我认为相对容易的任务,这给了我很多麻烦。我要进入雅虎!Weather RSS提要并尝试获取lastBuildDate并将该值存储在标签中。但是,当启动时,我在根级别获取的C# 解析RSS中的信息,c#,parsing,rss,C#,Parsing,Rss,我手头有一项我认为相对容易的任务,这给了我很多麻烦。我要进入雅虎!Weather RSS提要并尝试获取lastBuildDate并将该值存储在标签中。但是,当启动时,我在根级别获取的数据无效。第1行,位置1。参考GetBuild()中的最后一行: 我肯定我引用的lastBuildDate的位置不正确,但我不确定如何修复错误。在解析和获取外部信息方面,我是个新手 using System; using System.Collections.Generic; using System.Compone
数据无效。第1行,位置1。
参考GetBuild()中的最后一行:
我肯定我引用的lastBuildDate的位置不正确,但我不确定如何修复错误。在解析和获取外部信息方面,我是个新手
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
namespace WeatherApp
{
public partial class frmWeather : Form
{
string lastBuild;
public frmWeather()
{
InitializeComponent();
}
private void getBuild()
{
string query = string.Format("http://weather.yahooapis.com/forecastrss?w=2357473");
var lastBuild = XDocument.Parse(query).Root.Element("channel").Element("lastBuildDate").Value;
}
private void frmWeather_Load(object sender, EventArgs e)
{
getBuild();
lblLastBuild.Text = lastBuild;
}
}
}
以下是RSS源的格式:
<?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 - Aurora, CO</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Aurora__CO/*http://weather.yahoo.com/forecast/USCO0019_f.html</link>
<description>Yahoo! Weather for Aurora, CO</description>
<language>en-us</language>
<lastBuildDate>Fri, 14 Feb 2014 1:54 pm MST</lastBuildDate>
雅虎!天气-极光公司
http://us.rd.yahoo.com/dailynews/rss/weather/Aurora__CO/*http://weather.yahoo.com/forecast/USCO0019_f.html
雅虎!奥罗拉公司天气预报
恩美
2014年2月14日星期五下午1:54 MST
此处:
var lastBuild = XDocument.Parse(query).Root.Element("channel").Element("lastBuildDate").Value;
您试图解析的是Url
而不是XML内容。解析方法需要解析XML
内容。而是使用XDocument.Load(query)
或者先下载内容,然后对其进行解析:
using (var client = new WebClient())
{
string content = client.DownloadString("http://weather.yahooapis.com/forecastrss?w=2357473");
var lastBuild = XDocument.Parse(content).Root
.Element("channel")
.Element("lastBuildDate").Value;
}
要避免NullReferenceException
使用显式强制转换,而不是直接访问Value
属性。以下是带有空检查的更好版本:
using (var client = new WebClient())
{
string content = client.DownloadString("http://weather.yahooapis.com/forecastrss?w=2357473");
var rootElement = XDocument.Parse(content).Root;
if (rootElement != null)
{
var lastBuild = (string)rootElement.Element("channel")
.Element("lastBuildDate");
if (lastBuild != null)
{
// display value in the label
}
}
}
据我所知,您正在将url本身传递给
XDocument.Parse
,而不是通过HTTP GET返回的结果
所以你需要这样的东西
private void getBuild()
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://weather.yahooapis.com/forecastrss?w=2357473");
// make request and get string
string responseBody = inputStream.ReadToEnd();
var lastBuild = XDocument.Parse(responseBody).Root.Element("channel").Element("lastBuildDate").Value;
}
为了简洁起见,我省略了更多的HTTP代码。到这里来举一个完整的例子 我发现了一个旧的测试项目:
static void Main (string[] args)
{
XmlDocument doc = new XmlDocument ();
doc.Load ("http://xml.weather.yahoo.com/forecastrss?p=GMXX1791&u=c");
//doc.Save ("test.xml");
XmlNamespaceManager ns = new XmlNamespaceManager (doc.NameTable);
ns.AddNamespace ("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
XmlNodeList lastBuildDate = doc.SelectNodes ("//rss/channel/lastBuildDate", ns);
Console.WriteLine (lastBuildDate[0].InnerText);
XmlNodeList nodescity = doc.SelectNodes ("//rss/channel/title", ns);
foreach (XmlNode xnCity in nodescity)
{
Console.WriteLine (xnCity.InnerText);
}
XmlNodeList nodes = doc.SelectNodes ("//rss/channel/item/yweather:forecast", ns);
foreach (XmlNode node in nodes)
{
Console.WriteLine ("{0}: {1}, {2}C - {3}C",
node.Attributes["day"].InnerText,
node.Attributes["text"].InnerText,
node.Attributes["low"].InnerText,
node.Attributes["high"].InnerText);
}
}
你能提供相关的XML代码片段,这样我就可以编写XPath了吗?此外,该错误并不表示您的路径有问题,它表示数据不是有效的xml。与论坛网站不同,我们不使用“感谢”或“感谢任何帮助”或签名。看见“@evanmcdonnal谢谢你的回复。你得原谅我,我是个新手。我想我把RSS和XML的定义搞糊涂了。你把网页的内容放到
query
中了吗?看起来您正在解析url字符串itself@Joshua:这些网站与您习惯的网站不同。你感谢某人,是因为你对他们的答案投了更高的票,而不是因为那些空泛、毫无意义的话把问题弄得一团糟。请阅读我发布的链接。它的工作方式似乎比我所做的要好,但它返回的是一个空标签,而不是lastBuildDate标签中的内容……哇WebClient
比使用HttpRequest
简单得多,为什么我没有想到呢?@Joshua你确定吗?我试过了,效果很好。我得到了日期值。@Selman22我做错了什么。我是否可以获取您的全部源代码?@Joshua这是全部源代码。请用您的代码替换,然后重试
static void Main (string[] args)
{
XmlDocument doc = new XmlDocument ();
doc.Load ("http://xml.weather.yahoo.com/forecastrss?p=GMXX1791&u=c");
//doc.Save ("test.xml");
XmlNamespaceManager ns = new XmlNamespaceManager (doc.NameTable);
ns.AddNamespace ("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
XmlNodeList lastBuildDate = doc.SelectNodes ("//rss/channel/lastBuildDate", ns);
Console.WriteLine (lastBuildDate[0].InnerText);
XmlNodeList nodescity = doc.SelectNodes ("//rss/channel/title", ns);
foreach (XmlNode xnCity in nodescity)
{
Console.WriteLine (xnCity.InnerText);
}
XmlNodeList nodes = doc.SelectNodes ("//rss/channel/item/yweather:forecast", ns);
foreach (XmlNode node in nodes)
{
Console.WriteLine ("{0}: {1}, {2}C - {3}C",
node.Attributes["day"].InnerText,
node.Attributes["text"].InnerText,
node.Attributes["low"].InnerText,
node.Attributes["high"].InnerText);
}
}
string lastBuildDate = XDocument.Parse(query).Descendants("channel").Select(x => x.Element("lastBuildDate").Value).FirstOrDefault();