简体   繁体   中英

How to insert an element <item>..</item> inside the channel element in a RSS feed using C#?

I'm looking to create an rss feed for a website using C#. What I'm trying to approach is, once I clicked a button ("Add Feed"), it suppose to create and a Node Element, like this:

<item>
 <title> some title...here </title>
 <link> link to the article ...here </link>
 <description> article's description ...here </description>
</item>

This has to be a child of the <channel> element. So, so far with the code I wrote, it inserts the elements described above in the rss xml file, but it does it outside the <channel> element, like so:

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

This is the code :

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

How can I do that ? Any help or feedback will be appreciated. Thank you !!!

Try this, if i understood the requirements correctly:

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

Ok now that we got that the file is already saved somewhere, try this:

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

The .net framework has built in support for building RSS feeds in System.ServiceModel (you need a reference to System.ServiceModel.dll), it might be better to use it to guarantee valid RSS xml, and it's nicer to work with anyway that constructing the XML yourself. Futher reading on MSDN .

For example..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM