【问题标题】:XmL file, need to write to the end of the "channel" elementXmL 文件,需要写入“通道”元素的末尾
【发布时间】:2012-10-03 04:06:13
【问题描述】:

我正在寻找一个程序来为 iTunes Podcast 编写一个 rss 文件。我知道你可以买一个,但这是为了经验和非营利组织。这是我的 C# 代码

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 频道元素中,除了“项目”元素之外还有其他元素。(播客信息的其余部分)。我怎样才能将它附加在通道元素内但就在结束标记之前。这是 xml 文件的外观。谢谢,我是新人,请温柔...

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

我想在

之前添加

谢谢。

【问题讨论】:

    标签: c# xml rss itunes xelement


    【解决方案1】:

    这应该可行:

            doc.Root.Element("channel").Add(root);
    

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

    其他可能的方法是:

            doc.Element("rss").Element("channel").Add(root);
    

    或:

            var el = doc.Descendants("channel").FirstOrDefault();
            if (el != null)
                el.Add(root);
    

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

    【讨论】:

    • 谢谢伊万,我用了你的第二个例子。效果很好。
    猜你喜欢
    • 2014-06-30
    • 2011-04-14
    • 1970-01-01
    • 2022-07-22
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    相关资源
    最近更新 更多