I'm looking to create a program to write an rss file for iTunes Podcast. I know you can buy one, but this is for experience and for a non profit organization. here is my C# code
XDocument doc = XDocument.Load(fileLocation);
XNamespace itunes = "http://www.itunes.com/dtds/podcast-1.0.dtd";
XElement root = new XElement("item",
(new XElement("title", textBoxPodcastTitle.Text)),
(new XElement(itunes + "author", textBoxAuthor.Text)),
(new XElement(itunes + "subtitle", textBoxSubtitle.Text)),
(new XElement(itunes + "summary", textBoxSummary.Text)),
(new XElement("enclosuer",
new XAttribute("url", "\"http://www.jubileespanish.org/Podcast/\"" + textBoxFileName.Text + "\"" + " length=\"" + o_currentMp3File.Length.ToString() + "\" type=\"audio/mpeg\""))),
(new XElement("guid", "http://www.jubileespanish.org/Podcast/" + textBoxFileName.Text)),
(new XElement("pubDate", o_selectedMP3.currentDate())),
(new XElement(itunes + "duration", o_selectedMP3.MP3Duration(openFileDialogFileName.FileName.ToString()))),
(new XElement("keywords", textBoxKeywords.Text)));
doc.Element("channel").Add(root);
doc.Save(fileLocation);
everything works fine except when I am writing the root XElement I made. It cannot write it because in the iTunes channel element there are other elements besides the "item" elements.(The rest of the podcast info). How can I append it inside the channel element but right before the closing tag. Here is how the xml file looks like. Thanks, I'm new be gentle...
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<channel>
<title>Non Profit company</title>
<itunes:keywords>keywords</itunes:keywords>
<itunes:image href="http://www.podcast.org/Podcast/podcastlogo.png" />
<itunes:explicit>no</itunes:explicit>
<itunes:block>no</itunes:block>
<item>
<title>Red, Whine, & Blue</title>
<itunes:author>Various</itunes:author>
<itunes:subtitle>Red + Blue != Purple</itunes:subtitle>
<itunes:summary>This week we talk about surviving in a Red state if you are a Blue person. Or vice versa.</itunes:summary>
<itunes:image href="http://example.com/podcasts/everything/AllAboutEverything/Episode3.jpg" />
<enclosure url="http://example.com/podcasts/everything/AllAboutEverythingEpisode1.mp3" length="4989537" type="audio/mpeg" />
<guid>http://example.com/podcasts/archive/aae20050601.mp3</guid>
<pubDate>Wed, 1 Jun 2005 19:00:00 GMT</pubDate>
<itunes:duration>3:59</itunes:duration>
<itunes:keywords>politics, red, blue, state</itunes:keywords>
</item>
</channel>
</rss>
I would like to append right before the
thanks.
This should work:
doc.Root.Element("channel").Add(root);
Element retrieved by accessing the Root
property is rss
and the Add
method will add the element to the end of the element's content by default.
Other possible ways to do this would be:
doc.Element("rss").Element("channel").Add(root);
or:
var el = doc.Descendants("channel").FirstOrDefault();
if (el != null)
el.Add(root);
But the first one (using Root
property) would be the cleanest.