我正在寻找使用C#为网站创建RSS订阅源。我想要接近的是,一旦我点击一个按钮(“添加Feed”),它就会创建一个Node Element,如下所示:
<item>
<title> some title...here </title>
<link> link to the article ...here </link>
<description> article's description ...here </description>
</item>
这必须是&lt; channel&gt;的孩子。元件。因此,到目前为止,我编写的代码中,它将上述元素插入到rss xml文件中,但它在&lt; channel&gt;之外执行。元素,如:
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Example Home Page</title>
<link>http://www.example.com</link>
<description>Educational Website...</description>
<image>
<url>http://www.example.com/images/logo.png</url>
<title>Example.com</title>
<link>http://www.example.com</link>
</image>
<category>Photography</category>
<language>en-us</language>
</channel>
<item>
<title> some title...here </title>
<link> link to the article ...here </link>
<description> article's description ...here </description>
</item>
</rss>
这是代码:
XmlDocument doc = new XmlDocument();
XmlNode item = doc.CreateElement("item");
XmlNode Title = doc.CreateElement("title");
Title.InnerText = TextBoxTitle.Text;
item.AppendChild(Title);
XmlNode link = doc.CreateElement("link");
link.InnerText = "http://www.example.com/" + DropDownListCategory.SelectedItem.Text + ".aspx?key=" + TextBoxLink.Text + ".txt";
item.AppendChild(link);
XmlNode description = doc.CreateElement("description");
description.InnerText = TextBoxDescription.Text;
item.AppendChild(description);
doc.DocumentElement.AppendChild(item);
doc.Save(Server.MapPath("~/rss.xml"));
我该怎么做?任何帮助或反馈将不胜感激。谢谢!!!
答案 0 :(得分:2)
试试这个,如果我理解正确的要求:
XmlDocument doc = new XmlDocument();
XmlNode rss = doc.CreateElement("rss");
XmlAttribute version = doc.CreateAttribute("version");
version.Value = "2.0";
rss.Attributes.Append(version);
XmlNode channel = doc.CreateElement("channel");
XmlNode item = doc.CreateElement("item");
XmlNode Title = doc.CreateElement("title");
Title.InnerText = "Title Text";
item.AppendChild(Title);
XmlNode link = doc.CreateElement("link");
link.InnerText = "http://www.example.com/.txt";
item.AppendChild(link);
XmlNode description = doc.CreateElement("description");
description.InnerText = "DESC";
item.AppendChild(description);
channel.AppendChild(item);
rss.AppendChild(channel);
doc.AppendChild(rss);
doc.Save(Server.MapPath("~/rss.xml"));
好了,我们已经知道文件已经保存在某个地方,试试这个:
var xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("~/rss.xml"));
XmlNode channelNode = xmldoc.SelectSingleNode("descendant::channel");
if (channelNode != null)
{
XmlNode item = xmldoc.CreateElement("item");
XmlNode Title = xmldoc.CreateElement("title");
Title.InnerText = "Title Text";
item.AppendChild(Title);
XmlNode link = xmldoc.CreateElement("link");
link.InnerText = "http://www.example.com/.txt";
item.AppendChild(link);
XmlNode description = xmldoc.CreateElement("description");
description.InnerText = "DESC";
item.AppendChild(description);
channelNode.AppendChild(item);
}
doc.Save(Server.MapPath("~/rss.xml"));
答案 1 :(得分:1)
.net框架内置支持在System.ServiceModel
中构建RSS提要(您需要对System.ServiceModel.dll的引用),使用它来保证有效的RSS xml可能更好,而且&# 39;无论如何,自己构建XML更好。继续阅读on MSDN。
例如......
// read in the existing rss xml
var rssFeedXml = XDocument.Load(@"c:\temp\rss3.xml"); // replace with server.mappath etc
var feed = SyndicationFeed.Load(rssFeedXml.CreateReader());
var items = new List<SyndicationItem>(feed.Items);
feed.Items = items;
// add the new item
items.Add(
new SyndicationItem(
"some title...here ",
"some description here",
new Uri("http://example.come")));
// write the xml back out
var rssFormatter = new Rss20FeedFormatter(feed, false);
XDocument output = new XDocument();
using (var xmlWriter = output.CreateWriter())
rssFormatter.WriteTo(xmlWriter);
output.Save(@"c:\temp\rss.xml");