2011-11-18 3 views
1

으로 반환했습니다. friendfeed API에서 정보를 제공하려고합니다.C# .XML을 클래스

코드에서 볼 수 있듯이 HttpRequest를 사용하여 정보를 얻고 있습니다. 괜찮아.

그 후 나는 XML을 LINQ로 잘 읽는다.

하지만 "피드"클래스를 만들고 모든 반환 값 (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(); 
     } 
    } 
} 

답변

1

당신이 이런 식으로 읽을 생각한다면이 코드를 해보자 (동적 피드 클래스 - feed, entry, viafrom 클래스를 선언하지 않고) :

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.Netthis 확장 클래스

+0

이것은 linq 권리와 다른가요? (json.net) 내가 이것을 사용하려면 linq이 필요합니까. 사실 저는 새로운데, 이제 배울 것을 결정해야합니다. – NotNicolasCage

+0

아니 그것의 linq. 작성해야하는 모든 코드는 위와 같습니다. 아니 xml 구문 분석, 아니 클래스 선언. –

+0

흠, 고마워요. 이 표정이 더 유용하고 쉽습니다. 이제 우리는 많은 사이트 API를 사용하여 json을 사용합니다. 나는 지금 linq을 배울 필요가 없다. 다시 한 번 감사드립니다 :) – NotNicolasCage

0

ObservableCollection에 추가해야합니다.

1

의이

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

흠, 나는 이것을 사용할 수 있다고 생각합니다. 감사. – NotNicolasCage

+0

하지만, 나는 단지 하나의 엔트리 obj를 가질 것인가? – NotNicolasCage

+0

아니, 당신은 IEnumerable을 얻을 것이다