目前,我正在使用以下代码生成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;
}
}
如果我使用application/json; charset=utf-8
在javascript中请求此操作,我会自动获取json数据。
我的问题是我需要从外部RSS提要而不是本地数据库获取数据,并将其转换为json数据,以便我可以使用javascript以相同的方式调用它。
任何人都知道我如何使用上述类似的代码捕获RSS提要http://www.hotukdeals.com/rss/hot
?
答案 0 :(得分:5)
向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
答案 1 :(得分:0)
网络订阅源是用于向用户提供经常更新的内容的数据格式。假设您正在设计任何网站的主页,并且您想要显示来自某个热门或有用网站的供稿。在这种情况下,以下示例可能会有所帮助。
在此示例中,消耗的Feed显示在列表视图中。但您可以使用任何控件并相应地显示Feed。
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();
}
}
注意:我们应该始终关注从其他网站获取的数据。