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://ift.tt/1IW9t0J;
<description>Educational Website...</description>
<image>
<url>http://ift.tt/1tM4bkT;
<title>Example.com</title>
<link>http://ift.tt/1IW9t0J;
</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 !!!


No comments:

Post a Comment