c# - 将 RSS feed 转换为 DataTable

标签 c# .net xml datatable c#-3.0

您好,我正在阅读 RSS 提要并使用 DataTable 创建 XML。这是我的代码

  try
            {
                DataTable tbl = new DataTable();
                tbl.Columns.Add("id");
                tbl.Columns.Add("product_name");
                tbl.Columns.Add("description");

                //Extra Nodes
                tbl.Columns.Add("brand");
                tbl.Columns.Add("condition");
                tbl.Columns.Add("product_type");

                        XmlDocument doc = new XmlDocument();
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(s);
                        XmlNodeList itemNodes = xmlDoc.SelectNodes("//rss/channel/item");
                        foreach (XmlNode itemNode in itemNodes)
                        {
                            DataRow row = tbl.NewRow();
                            XmlNode idNode = itemNode.SelectSingleNode("id");
                            XmlNode product_nameNode = itemNode.SelectSingleNode("product_name");
                            XmlNode descriptionNode = itemNode.SelectSingleNode("description");

                            //extra nodes
                            XmlNode brandNode = itemNode.SelectSingleNode("brand");
                            XmlNode conditionNode = itemNode.SelectSingleNode("condition");
                            XmlNode product_typeNode = itemNode.SelectSingleNode("product_type");



                            if (idNode != null && product_nameNode != null && descriptionNode != null )
                            {
                                row[0] = idNode.InnerText;
                                row[1] = product_nameNode.InnerText;
                                row[2] = descriptionNode.InnerText;

                                //extra nodes
                                if (brandNode == null)
                                    row[3] = "";
                                else
                                    row[3] = brandNode.InnerText;

                                if (conditionNode==null)
                                    row[4] = "";
                                else
                                    row[4] = conditionNode.InnerText;

                                if (product_typeNode==null)
                                    row[5] = "";
                                else
                                    row[5] = product_typeNode.InnerText;

                                                          }
                            tbl.Rows.Add(row);
                            // tbl.Rows.Add(row);
                        }

                }
            }
            catch (Exception ex)
            {
               // Console.WriteLine(ex.Message);
               // Console.Read();

            }

这工作正常,没有任何问题,但我想让我的代码更高效。这是读取 Rss 并将其添加到数据表中的好方法吗?我正在 VS 2008 上创建一个 SSIS 项目,因此我无法使用 SyndicateFeed 。

最佳答案

您可以使用下面的代码并用作示例。

using System;
using System.ServiceModel.Syndication;
using System.Xml;

namespace RSSFeed
{
    public class Program
    {
        static void Main(string[] args)
        {
            // URL from the site you need (RSS Feed in XML please).
            String url = "http://www.medicalnewstoday.com/rss/abortion.xml";

            // Create XML Reader.
            using (XmlReader xmlReader = XmlReader.Create(url, new XmlReaderSettings() { DtdProcessing = DtdProcessing.Ignore }))
            {
                // Load The Feed.
                SyndicationFeed syndicationFeed = SyndicationFeed.Load(xmlReader);

                // through the list.
                foreach (SyndicationItem item in syndicationFeed.Items)
                {
                    // You can use a lot of information here todo what you need.
                    // TODO...

                    // Examples
                    String subject = item.Title.Text;
                    String summary = item.Summary.Text;
                }

                xmlReader.Close();
            }
        }
    }
}

关于c# - 将 RSS feed 转换为 DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28311324/

相关文章:

c# - 了解 XAML/WPF 中的样式和模板

c++ - 使用常量内存使用的 xml 解析

java - Stax问题解析文档,其结束元素和开始元素在同一行

层列表中的 Android 项目大小

c# - 设置 DisplayMember 后 DataGridViewComboBoxColumn 会发生什么情况?

c# - MemoryStream 到 string[]

c# - 调用被被调用者拒绝

c# - 安全登录表格和注册表(带序列号)

c# - 我可以将枚举添加到现有的 .NET 结构中,例如日期吗?

c# - 向 OData(v4) Web API C# 添加自定义分页