C#如何正确调用此代码

时间:2013-12-05 18:04:13

标签: c# visual-studio-2010 api

我正在尝试将此TwitchTv API XML代码集成到我自己的项目中,但当我尝试在getStreamInfo中调用button1_Click event时,我无法使其工作。

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.Xml.Linq;

namespace jsontest //change this if you want
{
public class twitchTVAPIModel
{
    public string Title { get; set; }
    public string ChannelOwner { get; set; }
    public string ChannelUrl { get; set; }
    public int ViewerCount { get; set; }
    public string Status { get; set; }
    public string Username { get; set; }
    public string GameTitle { get; set; }
}
public class twitch
{
    public List<twitchTVAPIModel> getStreamInfo(string channel)
    {
        string uriEndPoint = "http://api.justin.tv/api/stream/list.xml?channel=" + channel;
        var xmlDocument = XDocument.Load(uriEndPoint);
        var streams = (from stream in xmlDocument.Descendants("stream")
                       select new twitchTVAPIModel()
                       {
                           Title = stream.Element("title").Value,
                           ViewerCount = Convert.ToInt32(stream.Element("stream_count").Value),
                           Status = stream.Element("status").Value,
                           Username = stream.Element("channel").Element("login").Value,
                           ChannelUrl = stream.Element("channel").Element("channel_url").Value,
                           GameTitle = stream.Element("channel").Element("meta_game").Value
                       }).ToList<twitchTVAPIModel>();
        return streams;
    }
}
}

该行:

public List<twitchTVAPIModel> getStreamInfo(string channel)

我不清楚,这也是我不理解如何正确调用它的一个原因。

1 个答案:

答案 0 :(得分:3)

该行

public List<twitchTVAPIModel> getStreamInfo(string channel)

方法标题是声明函数getStreamInfo接受string作为唯一参数,并将返回ListtwitchTVAPIModel个对象

编辑:由于该函数不是静态的,因此在调用此函数之前,您需要实例化Twitch对象

var twitch = new Twitch();
var results = twitch.getStreamInfo("someChannel");