Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将RSS提要转换为数据表_C#_.net_Xml_Datatable_C# 3.0 - Fatal编程技术网

C# 将RSS提要转换为数据表

C# 将RSS提要转换为数据表,c#,.net,xml,datatable,c#-3.0,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");

您好,我正在阅读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并添加到数据表中的好方法吗?我正在VS2008上制作一个SSIS项目,因此无法使用SyndicationFeed。

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

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

请看这里抱歉我不能在VS 2008 SSIS项目上使用SyndicationFeed谢谢,但我不能在SSIS 2008项目@Diego上使用SyndicationFeed