Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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# 如何让.NET3.5基于RSS提要自动生成JSON数据?_C#_Json_Rss_C# 3.0_Asp.net 3.5 - Fatal编程技术网

C# 如何让.NET3.5基于RSS提要自动生成JSON数据?

C# 如何让.NET3.5基于RSS提要自动生成JSON数据?,c#,json,rss,c#-3.0,asp.net-3.5,C#,Json,Rss,C# 3.0,Asp.net 3.5,目前,我正在使用以下代码生成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() { }

目前,我正在使用以下代码生成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;
    }
}
公共类应用程序
{
公共字符串app_name;
公共字符串应用程序路径;
公共应用程序(字符串m_应用程序名称,字符串m_应用程序路径)
{
app_name=m_app_name;
app_path=m_app_path;
}
公共应用程序(){}
}
[脚本服务]
公共类应用程序:WebService{
列表应用程序=新列表();
SqlConnection连接;
SqlCommand命令;
SqlDataReader;
[WebMethod()]
公共列表GetUserApps()
{
var apps=新列表();
使用(connection=newsqlconnection(ConfigurationManager.AppSettings[“connString”]))
{
使用(command=newsqlcommand(@“此处有一些查询”,连接))
{
connection.Open();
使用(reader=command.ExecuteReader())
{
int-AppNameIndex=reader.GetOrdinal(“应用程序名称”);
int-AppPathIndex=reader.GetOrdinal(“应用程序路径”);
while(reader.Read())
{
apps.Add(新应用程序(reader.GetString(AppNameIndex)、reader.GetString(AppPathIndex));
}
}
}
}
返回应用程序;
}
}
如果我随后使用
application/json在javascript中请求它;charset=utf-8
,我自动获取json数据

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


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

添加对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;
    }
然后将其添加到foreach循环中(转换第一个日期)

var dates=item.ElementExtensions.Where(x=>x.OuterName==“date”);
如果(dates.Count()>0)
{
var元素=dates.First().GetObject();
var d=DateTime.Parse(element.Value);
}

我在本文中找到了它:

向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;
    }
然后将其添加到foreach循环中(转换第一个日期)

var dates=item.ElementExtensions.Where(x=>x.OuterName==“date”);
如果(dates.Count()>0)
{
var元素=dates.First().GetObject();
var d=DateTime.Parse(element.Value);
}

我在本文中找到了它:

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();
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Xml;
使用系统数据;
使用System.ServiceModel.Syndication;//用于feed实现
公共部分类FeedConsume:System.Web.UI.Page
{
/// 
///从该函数调用listview的databind函数。
/// 
/// 
/// 
受保护的无效页面加载(对象发送方、事件参数e)
{
//查看提要数据是否在缓存中
IEnumerable items=缓存[“提要”]作为IEnumerable;
if(items==null)
{
//如果缓存中不存在,则从发件人的网站获取
尝试
{
SyndicationFeed sfFeed=SyndicationFeed.Load(XmlReader.Create(“发件人网站的提要URL”);
//每个网站都有一个静态url,其中包含他们的提要,我们可以从该url获取提要。
items=sfFeed.items;
}
抓住
{
项目=新列表();
}
//将项目添加到缓存中
Cache.Insert(“提要”,items,null,DateTime.Now.AddHours(1.0),TimeSpan.Zero);
}
//正在创建datatable以将其绑定到listview。此datatable将包含发件人网站的所有源。
DataTable dtItems=新DataTable();
dtItems.Columns.Add(“Title”,typeof(string));
添加(“链接”,typeof(字符串));
foreach(SyndicationItem SyndicationItems in items)
{
dtItems.Rows.Add(synItem.Title.Text,synItem.Links[0].Uri.ToString());
}
FeedList.DataSource=dtItems;
FeedList.DataBind();
}
/// 
///为listView中的控件指定了适当的值
/// 
/// 
/// 
受保护的void FeedList_ItemDataBound(对象发送方,ListViewItemEventArgs e)
{
ListViewDataItem lvDataItem=(ListViewDataItem)e.Item;
DataRowView drvItem=(DataRowView)lvDataItem.DataItem;
标签标题=(标签)e.Item.FindControl(“标题”);
title.Text=drvItem[“title”].ToString();
超级链接=(超级链接)e.Item.FindControl(“链接”);
link.Text=drvItem[“link”].ToString();
link.NavigateUrl=drvItem[“link”].ToString();
}
}

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

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

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