将RSS提要转换为DataTable

时间:2015-02-04 00:20:42

标签: 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项目,所以我不能使用SyndicationFeed。

1 个答案:

答案 0 :(得分:2)

您可以使用以下代码并将其作为示例。

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();
            }
        }
    }
}