【问题标题】:How to insert an element <item>..</item> inside the channel element in a RSS feed using C#?如何使用 C# 在 RSS 提要的频道元素内插入元素 <item>..</item>?
【发布时间】:2015-03-31 03:53:42
【问题描述】:

我正在寻找使用 C# 为网站创建 rss 提要。我想要解决的是,一旦我点击了一个按钮(“添加提要”),它就会创建一个节点元素,如下所示:

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

这必须是 元素的子元素。因此,到目前为止,我编写的代码将上述元素插入到 rss xml 文件中,但它在 元素之外执行,如下所示:

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

这是代码:

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

我该怎么做?任何帮助或反馈将不胜感激。谢谢!!!

【问题讨论】:

    标签: c# asp.net xml rss


    【解决方案1】:

    如果我正确理解要求,试试这个:

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

    好的,现在我们知道文件已经保存在某个地方了,试试这个:

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

    【讨论】:

    • 把大事加到底的好习惯。
    • 感谢您帮助我。我测试了您的代码,虽然它做了我想要的,即在 节点内添加 元素。这种方法的问题在于,每次单击按钮添加新内容时,旧内容都会被删除。我喜欢保持一切完好无损,只需添加新的 .... 元素。就像我用我的方法做的那样。我做的不对,是将这些元素 添加到 节点内。我不得不说我已经手动创建了 rss.xml 结构,我唯一想做的编程就是添加 节点。
    【解决方案2】:

    .net 框架已内置支持在 System.ServiceModel 中构建 RSS 提要(您需要引用 System.ServiceModel.dll),使用它来保证有效的 RSS xml 可能会更好,而且工作起来更好无论如何,自己构建 XML。进一步阅读on MSDN

    例如..

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

    【讨论】:

      猜你喜欢
      • 2019-01-23
      • 2018-07-04
      • 1970-01-01
      • 2017-08-30
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      相关资源
      最近更新 更多