1

目前,我正在使用以下代码生成 xml 和 json 数据:

public class App
{
    public string app_name;
    public string app_path;

    public App(string m_app_name, string m_app_path)
    {
        app_name = m_app_name;
        app_path = m_app_path;
    }

    public App() { }
}

[ScriptService]
public class Apps : WebService {
    List<App> App = new List<App>();

    SqlConnection connection;
    SqlCommand command;
    SqlDataReader reader;

    [WebMethod()]
    public List<App> GetUserApps()
    {
        var apps = new List<App>();

        using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
        {


                using (command = new SqlCommand(@"some query here", connection))
                {
                    connection.Open();
                    using (reader = command.ExecuteReader())
                    {
                        int AppNameIndex = reader.GetOrdinal("application_name");
                        int AppPathIndex = reader.GetOrdinal("application_path");

                        while (reader.Read())
                        {
                            apps.Add(new App(reader.GetString(AppNameIndex), reader.GetString(AppPathIndex)));
                        }
                    }
                }



        }

        return apps;
    }
}

如果我随后在 javascript 中使用 请求这个application/json; charset=utf-8,我会自动获取 json 数据。

我的问题是我需要从外部 RSS 提要而不是本地数据库获取数据,并将其转换为 json 数据,这样我就可以使用 javascript 以同样的方式调用它。

任何人都知道如何http://www.hotukdeals.com/rss/hot使用与上述类似的代码来捕获 RSS 提要?

4

2 回答 2

6

添加对 System.ServiceModel.Web 的引用并使用以下代码:

    [WebMethod()]     
    public List<Deal> GetDeals()
    {
        var deals = new List<Deal>();

        XmlReader xr = XmlReader.Create("http://www.hotukdeals.com/rss/hot");
        SyndicationFeed feed = SyndicationFeed.Load(xr);

        foreach (var item in feed.Items)
        {
            deals.Add(new Deal { Title = item.Title.Text, Summary = item.Summary.Text });
        }

        return deals;
    }

将上面的 Deal 类替换为您的应用所需的任何类,并添加一些错误处理:)

有关详细信息,请查看SyndicationFeed文档。


要读取dc:date属性,请添加

using System.Xml.Linq;

然后在 foreach 循环中添加它(转换第一个日期)

        var dates = item.ElementExtensions.Where(x => x.OuterName == "date");

        if(dates.Count() > 0) 
        {
            var element = dates.First().GetObject<XElement>();
            var d = DateTime.Parse(element.Value);
        }

我在这篇文章中找到了它:Reading non-standard elements in a SyndicationItem with SyndicationFeed

于 2012-07-09T22:01:30.667 回答
0

Web 提要是一种数据格式,用于为用户提供经常更新的内容。假设您正在设计任何网站的主页,并且想要显示来自某个流行或有用网站的提要。在这种情况下,以下示例可能会有所帮助。

在此示例中,使用的提要显示在列表视图中。但是您可以使用任何控件并相应地显示提要。

listview 的脚本是:

<div>
    <asp:ListView ID="FeedList" runat="server" 
            onitemdatabound="FeedList_ItemDataBound" DataKeyNames="Title,Link" >
        <LayoutTemplate > 
           <div runat="server" id="ItemPlaceHolder">
           </div>
        </LayoutTemplate>
        <ItemTemplate>
            <asp:Label ID="title" runat="server"></asp:Label>
            <br />
            <asp:HyperLink ID="link" runat="server"></asp:HyperLink>
            <br />
        </ItemTemplate>
    </asp:ListView>
</div>

该示例的文件背后的代码是:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
using System.ServiceModel.Syndication; // used for feed implementation

public partial class FeedConsume : System.Web.UI.Page
{
    /// <summary>
    /// databind function of listview is called from this function.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        // See if feed data is in the cache
        IEnumerable<SyndicationItem> items = Cache["Feeds"] as IEnumerable<SyndicationItem>;
        if (items == null)
        {
            // If not present in cache then get it from sender's website
            try
            {
                SyndicationFeed sfFeed = SyndicationFeed.Load(XmlReader.Create("FEED URL OF senders website"));

                // every website has a static url which contains their feed and we can get the feed from that url. 
                items = sfFeed.Items;
                }
            catch
            {
                items = new List<SyndicationItem>();
            }
            // Add the items to the cache
            Cache.Insert("Feeds", items, null, DateTime.Now.AddHours(1.0),TimeSpan.Zero);
        }

        // Creating the datatable to bind it to the listview. This datatable will contain all the feeds from the senders website.
        DataTable dtItems = new DataTable();
        dtItems.Columns.Add("Title", typeof(string));
        dtItems.Columns.Add("Link", typeof(string));
        foreach (SyndicationItem synItem in items)
        {
            dtItems.Rows.Add(synItem.Title.Text, synItem.Links[0].Uri.ToString());
        }
        FeedList.DataSource = dtItems;
        FeedList.DataBind(); 
    }

    /// <summary>
    /// Controls in listView are assigned proper value
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void FeedList_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        ListViewDataItem lvDataItem = (ListViewDataItem)e.Item;
        DataRowView drvItem = (DataRowView)lvDataItem.DataItem;
        Label title = (Label)e.Item.FindControl("title");
        title.Text = drvItem["Title"].ToString();
        HyperLink link = (HyperLink)e.Item.FindControl("link");
        link.Text = drvItem["Link"].ToString();
        link.NavigateUrl = drvItem["Link"].ToString();
    }
}

注意:我们应该始终关心从其他网站获得的数据。

于 2012-07-11T07:13:05.843 回答