Repeater 上的 Rss 提要出错
Rss feed on a Repeater is giving an error
我正在尝试在 Repeater(或 Gridview)上解析 Rss 提要,但我做不到。保持 "Data at the root level is invalid. Line 1, position 1." 错误。
XmlDocument rssXmlDoc = new XmlDocument();
// Load the RSS file from the RSS URL
rssXmlDoc.Load("http://feeds.skynews.com/feeds/rss/home.xml");
// Parse the Items in the RSS file
XmlNodeList rssNodes = rssXmlDoc.SelectNodes("rss/channel/item");
StringBuilder rssContent = new StringBuilder();
// Iterate through the items in the RSS file
foreach (XmlNode rssNode in rssNodes)
{
XmlNode 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 description = rssSubNode != null ? rssSubNode.InnerText : "";
// rssContent.Append("<a href='" + link + "'>" + title + "</a><br>" + description);
rssContent.Append(description);
}
// Return the string that contain the RSS items
DataSet ds = new DataSet();
XmlTextReader reader = new XmlTextReader(new StringReader(rssContent.ToString()));
ds.ReadXml(reader);
rssRepeater.DataSource = ds.Tables[2];
rssRepeater.DataBind();
由于这行代码,您遇到异常:
XmlTextReader reader = new XmlTextReader(new StringReader(rssContent.ToString()));
ds.ReadXml(reader);
该行为是预期的,因为您试图将 rssContent
读作 XML 而 不是 。
这是你构建后的rssContent
StringBuilder
:
Patients will have to wait longer for non-urgent operations and go
without some new drugs under plans to be announced today.The European
Union's approach to talks which will shape its future relationship
with the UK is set to be laid out by European Council president Donald
Tusk.Donald Trump's former national security adviser has reportedly
offered to testify at hearings into alleged Russian meddling in the US
election in exchange for immunity.Islamic State is using civilians as
bait for coalition air strikes to create public outcry as the
offensive in Mosul continues.Scotland's First Minister Nicola Sturgeon
will tell Theresa May in a letter that they need to make a start on
arrangements for an independence referendum.Excitement is building for
the latest instalment of Game Of Thrones after a new trailer for the
show's seventh season was released.Facebook, Google, Twitter and
Microsoft have said they will ramp up efforts to fight terrorist
content after a meeting with the Home Secretary.A former Power Rangers
star has been jailed for six years after killing his roommate with a
sword.Five people who died in a helicopter crash in Snowdonia were all
from the same family, police have confirmed.Westminster terror killer
Khalid Masood died from a single shot to his chest, an inquest has
been told.
为了防止这个问题,你必须构造有效的rssContent
XML,或者你需要完全避免使用XmlTextReader
.
我个人会使用 XmlSerializer
反序列化 XML,然后我会将其作为 DataSource
传递给转发器。
但是,这是可能的解决方案,使用您通过 XmlDocument
的方法。首先,您需要创建 class 哪个集合将成为您中继器的 DataSource
。
public class FeedItem
{
public string Title { get; set; }
public string Link { get; set; }
public string Description { get; set; }
}
然后,当您遍历节点时,您需要填充 FeedItem
集合并将该集合传递给 repeater
。
List<FeedItem> feedItems = new List<FeedItem>();
foreach (XmlNode rssNode in nodes)
{
var feedItem = new FeedItem();
XmlNode rssSubNode = rssNode.SelectSingleNode("title");
feedItem.Title = rssSubNode != null ? rssSubNode.InnerText : "";
rssSubNode = rssNode.SelectSingleNode("link");
feedItem.Link = rssSubNode != null ? rssSubNode.InnerText : "";
rssSubNode = rssNode.SelectSingleNode("description");
feedItem.Description = rssSubNode != null ? rssSubNode.InnerText : "";
// rssContent.Append("<a href='" + link + "'>" + title + "</a><br>" + description);
feedItems.Add(feedItem);
}
rssRepeater.DataSource = feedItems; //Repeater datasource is list of FeedItem objects.
rssRepeater.DataBind();
我正在尝试在 Repeater(或 Gridview)上解析 Rss 提要,但我做不到。保持 "Data at the root level is invalid. Line 1, position 1." 错误。
XmlDocument rssXmlDoc = new XmlDocument();
// Load the RSS file from the RSS URL
rssXmlDoc.Load("http://feeds.skynews.com/feeds/rss/home.xml");
// Parse the Items in the RSS file
XmlNodeList rssNodes = rssXmlDoc.SelectNodes("rss/channel/item");
StringBuilder rssContent = new StringBuilder();
// Iterate through the items in the RSS file
foreach (XmlNode rssNode in rssNodes)
{
XmlNode 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 description = rssSubNode != null ? rssSubNode.InnerText : "";
// rssContent.Append("<a href='" + link + "'>" + title + "</a><br>" + description);
rssContent.Append(description);
}
// Return the string that contain the RSS items
DataSet ds = new DataSet();
XmlTextReader reader = new XmlTextReader(new StringReader(rssContent.ToString()));
ds.ReadXml(reader);
rssRepeater.DataSource = ds.Tables[2];
rssRepeater.DataBind();
由于这行代码,您遇到异常:
XmlTextReader reader = new XmlTextReader(new StringReader(rssContent.ToString()));
ds.ReadXml(reader);
该行为是预期的,因为您试图将 rssContent
读作 XML 而 不是 。
这是你构建后的rssContent
StringBuilder
:
Patients will have to wait longer for non-urgent operations and go without some new drugs under plans to be announced today.The European Union's approach to talks which will shape its future relationship with the UK is set to be laid out by European Council president Donald Tusk.Donald Trump's former national security adviser has reportedly offered to testify at hearings into alleged Russian meddling in the US election in exchange for immunity.Islamic State is using civilians as bait for coalition air strikes to create public outcry as the offensive in Mosul continues.Scotland's First Minister Nicola Sturgeon will tell Theresa May in a letter that they need to make a start on arrangements for an independence referendum.Excitement is building for the latest instalment of Game Of Thrones after a new trailer for the show's seventh season was released.Facebook, Google, Twitter and Microsoft have said they will ramp up efforts to fight terrorist content after a meeting with the Home Secretary.A former Power Rangers star has been jailed for six years after killing his roommate with a sword.Five people who died in a helicopter crash in Snowdonia were all from the same family, police have confirmed.Westminster terror killer Khalid Masood died from a single shot to his chest, an inquest has been told.
为了防止这个问题,你必须构造有效的rssContent
XML,或者你需要完全避免使用XmlTextReader
.
我个人会使用 XmlSerializer
反序列化 XML,然后我会将其作为 DataSource
传递给转发器。
但是,这是可能的解决方案,使用您通过 XmlDocument
的方法。首先,您需要创建 class 哪个集合将成为您中继器的 DataSource
。
public class FeedItem
{
public string Title { get; set; }
public string Link { get; set; }
public string Description { get; set; }
}
然后,当您遍历节点时,您需要填充 FeedItem
集合并将该集合传递给 repeater
。
List<FeedItem> feedItems = new List<FeedItem>();
foreach (XmlNode rssNode in nodes)
{
var feedItem = new FeedItem();
XmlNode rssSubNode = rssNode.SelectSingleNode("title");
feedItem.Title = rssSubNode != null ? rssSubNode.InnerText : "";
rssSubNode = rssNode.SelectSingleNode("link");
feedItem.Link = rssSubNode != null ? rssSubNode.InnerText : "";
rssSubNode = rssNode.SelectSingleNode("description");
feedItem.Description = rssSubNode != null ? rssSubNode.InnerText : "";
// rssContent.Append("<a href='" + link + "'>" + title + "</a><br>" + description);
feedItems.Add(feedItem);
}
rssRepeater.DataSource = feedItems; //Repeater datasource is list of FeedItem objects.
rssRepeater.DataBind();