C# 将 .XML 返回给类

发布于 2024-12-17 06:17:02 字数 1853 浏览 3 评论 0 原文

我试图提供来自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();
        }
    }
}

I am tring to give an information from friendfeed API.

As you see in code, I am using an HttpRequest to get information. It's ok.

After that I am reading XML just fine with LINQ.

But now I create a "feed" class and I want to create an object for every returned value (i from finaltoclass).

How can I do this?

Can you help me with this?

Thank you.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必了 2024-12-24 06:17:02

如果您想这样阅读(动态提要类 - 不声明 feedentryviafrom类):

dynamic feed = new Uri("http://friendfeed-api.com/v2/feed/" + username + "?format=json").GetDynamicJsonObject();
foreach (var entry in feed.entries)
{
    Console.WriteLine(entry.from.name + "> " +  entry.body + " " + entry.url);
}

您将需要 Json.Net这个扩展类

If you think to read it this way (dynamic feed class - without declaring feed, entry, via and from classes ):

dynamic feed = new Uri("http://friendfeed-api.com/v2/feed/" + username + "?format=json").GetDynamicJsonObject();
foreach (var entry in feed.entries)
{
    Console.WriteLine(entry.from.name + "> " +  entry.body + " " + entry.url);
}

you will need Json.Net and this extension class

罪#恶を代价 2024-12-24 06:17:02

我们来试试这段代码

 var finaltoclass = from i in final.Elements("entry")
    select new entry (i.Element("body").Value, i.Element("id").Value, i.Element("url").Value );

Let's try this code

 var finaltoclass = from i in final.Elements("entry")
    select new entry (i.Element("body").Value, i.Element("id").Value, i.Element("url").Value );
束缚m 2024-12-24 06:17:02

您需要添加到条目的 ObservableCollection 中。

You need to add to an ObservableCollection of entry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文