C# 将 .XML 返回给类
我试图提供来自friendfeed API 的信息。
正如您在代码中看到的,我正在使用 HttpRequest 来获取信息。没关系。
之后,我可以使用 LINQ 很好地读取 XML。
但现在我创建了一个“feed”类,我想为每个返回值(i 来自finaltoclass)创建一个对象。
我该怎么做?
你能帮我解决这个问题吗?
谢谢。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
class feed { }
public class entry {
string body;
string id;
string url;
public entry(string a, string b, string c)
{
body = a;
id = b;
url = c;
}
}
static void Main(string[] args)
{
string username = "semihmasat";
WebRequest ffreq = WebRequest.Create("http://friendfeed-api.com/v2/feed/" + username + "?format=xml");
WebResponse ffresp = ffreq.GetResponse();
Console.WriteLine(((HttpWebResponse)ffresp).StatusDescription);
Stream stream = ffresp.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string respfinal = reader.ReadToEnd();
reader.Close();
XElement final = XElement.Load("http://friendfeed-api.com/v2/feed/" + username + "?format=xml");
var finaltoclass = from i in final.Elements("entry")
select i;
foreach (XElement i in finaltoclass) {
string body= i.Element("body").Value;
string id= i.Element("id").Value;
string url= i.Element("url").Value;
Console.WriteLine("{0},{1},{2}", body, id, url);
}
Console.ReadLine();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想这样阅读(动态提要类 - 不声明
feed
、entry
、via
和from
类):您将需要 Json.Net 和 这个扩展类
If you think to read it this way (dynamic feed class - without declaring
feed
,entry
,via
andfrom
classes ):you will need Json.Net and this extension class
我们来试试这段代码
Let's try this code
您需要添加到条目的
ObservableCollection
中。You need to add to an
ObservableCollection
of entry.