C # Returned .XML to class

I am trying to provide information from friendfeed's API.

As you can see in the code, I use HttpRequest to get the information. This is normal.

After that I read XML perfectly with LINQ.

But now I am creating a "feed" class, and I want to create an object for each return value (i from finaltoclass).

How can i do this?

Can you help me?

Thanks.

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
source share
3 answers

If you are thinking of reading it like this (dynamic feed class - no declarations of 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

+1
source

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 ); 
+1
source

You need to add an ObservableCollection entry.

0
source

Source: https://habr.com/ru/post/1382783/


All Articles