Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.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#将.XML返回到类_C#_Linq To Xml_Xml Parsing - Fatal编程技术网

C#将.XML返回到类

C#将.XML返回到类,c#,linq-to-xml,xml-parsing,C#,Linq To Xml,Xml Parsing,我想从FriendFeedAPI提供一个信息 正如您在代码中看到的,我正在使用HttpRequest获取信息。没关系 在这之后,我对LINQ的XML理解还不错 但是现在我创建了一个“feed”类,我想为每个返回值(我来自finaltoclass)创建一个对象 我该怎么做 你能帮我吗 多谢各位 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net;

我想从FriendFeedAPI提供一个信息

正如您在代码中看到的,我正在使用HttpRequest获取信息。没关系

在这之后,我对LINQ的XML理解还不错

但是现在我创建了一个“feed”类,我想为每个返回值(我来自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();
        }
    }
}

您需要添加到条目的
可观察集合中。

让我们试试这段代码

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

如果您想以这种方式阅读(动态提要类-不声明
feed
entry
via
from
类):


您将需要和扩展类

这与linq不同,对吗?(json.net)我是否需要linq来使用这个。事实上,我是新来的,现在我必须决定学什么。不,不是林克。您必须编写的所有代码如上所述。没有xml解析,没有类声明。嗯,谢谢。这看起来更有用、更简单。现在我看到很多网站api使用json。我现在不需要学习linq。再次感谢:)谢谢。最后一个问题是询问您提供链接的扩展(而不是json.net)。它能做什么?只需通过复制粘贴将其添加到您的项目中即可。它使用json.Net解析json结果,并返回一个
动态
对象。不,您将得到IEnumerable
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);
}