osh*_*nen 0 c# rss json asp.net-3.5 c#-3.0
目前,我使用以下代码生成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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我然后在javascript中使用请求application/json; charset=utf-8,我会自动获取json数据.
我的问题是我需要从外部RSS提要而不是本地数据库获取数据,并将其转换为json数据,以便我可以使用javascript以相同的方式调用它.
任何人都知道我如何http://www.hotukdeals.com/rss/hot使用类似的代码捕获RSS提要?
添加对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;
}
Run Code Online (Sandbox Code Playgroud)
将上面的Deal类替换为您的应用程序所需的任何类,并添加一些错误处理:)
有关详细信息,请查看SyndicationFeed文档.
要阅读dc:date属性,请添加
using System.Xml.Linq;
Run Code Online (Sandbox Code Playgroud)
然后在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);
}
Run Code Online (Sandbox Code Playgroud)
我在本文中找到了它:使用SyndicationFeed读取SyndicationItem中的非标准元素
| 归档时间: |
|
| 查看次数: |
1233 次 |
| 最近记录: |