c# - 如何使用 C# 在 RSS 提要的 channel 元素内插入元素 <item>..</item>?

标签 c# asp.net xml rss

我正在寻找使用 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"));

我怎样才能做到这一点?任何帮助或反馈将不胜感激。谢谢!!!

最佳答案

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

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

关于c# - 如何使用 C# 在 RSS 提要的 channel 元素内插入元素 <item>..</item>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28256787/

相关文章:

java - 在 Java 中解析此 XML 的最简单方法?

asp.net - web api TaskScheduler.UnobservedTaskException

c# - asp.net 中的动态下拉列表

c# - 在 ASP.NET 中用 C# 将文件写入响应

c# - 无法在 asp.net core 的 startup.cs 文件中找到 Use.RunTimePageInfo() 方法

java - Spring 3.0 中获取 servlet 异常

xml - 将 Excel 工作表另存为 xml 在某些行周围添加引号

c# - 递归地获取 Active Directory 组的成员,即包括子组

c# - 在 C# 中处理大型 CDATA 部分

c# - 高性能异步等待套接字