首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XmL文件中,需要写入"channel“元素的末尾

XmL文件中,需要写入"channel“元素的末尾
EN

Stack Overflow用户
提问于 2012-10-12 18:49:12
回答 1查看 425关注 0票数 2

我希望创建一个程序来编写一个iTunes播客的rss文件。我知道你可以买一个,但这是为了经验和非营利组织。下面是我的C#代码

代码语言:javascript
复制
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);

除了我编写自己创建的根XElement之外,一切都运行得很好。它无法写入它,因为在iTunes通道元素中,除了"item“元素之外还有其他元素。(播客信息的其余部分)。如何才能将其添加到channel元素内,但恰好在结束标记之前。下面是xml文件的外观。谢谢,我是新来的.

代码语言:javascript
复制
<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, &amp; 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>

我想在前面加上

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-12 19:03:23

这应该是可行的:

代码语言:javascript
复制
        doc.Root.Element("channel").Add(root);

通过访问Root属性检索到的元素是rss,默认情况下,Add方法会将该元素添加到元素内容的末尾。

其他可能的方法是:

代码语言:javascript
复制
        doc.Element("rss").Element("channel").Add(root);

或者:

代码语言:javascript
复制
        var el = doc.Descendants("channel").FirstOrDefault();
        if (el != null)
            el.Add(root);

但是第一个(使用Root属性)将是最干净的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12857405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档