Atom xml readable with c #

I don’t know about this at all, I have a built-in page containing a gridview for describing the daily input, divided into columns from different authors, it looks like an excel file. And below is the Atom link.

If I click on a link to one line, especially the author of the message, I will be redirected to the author’s properties page, which will contain the name, current work and how much he wrote his book (50/70 80% is called status, etc.), interestingly, how can I read this information and display it in another view of the corresponding application; that is, I only know the URL of the channel, I really don’t know how to do this. Thanks for any help.

+4
source share
2 answers

I did not try to use DataContractSerializer with specialized XML formats, but the XmlSerializer allows you to set the attribute and element. This is the easiest way, as far as I can tell, because you can create a beautiful object model and use it to read any XML. Here's a PRIVATE example of reading an atom feed. You will need to do an HttpWebRequest to get the XML (which is pretty straight forward) and then use the XmlSerializer to deserialize the feed.

[XmlType(TypeName = "feed", Namespace = "http://www.w3.org/2005/Atom")] public class Feed { [XmlElement(ElementName = "title")] public string Title { get; set; } [XmlElement(ElementName = "updated")] public DateTime? Updated { get; set; } [XmlElement(ElementName = "id")] public string Id { get; set; } [XmlElement(ElementName = "link")] public Link Link { get; set; } [XmlElement(ElementName = "entry")] public List<Entry> Entries { get; set; } public Feed() { Entries = new List<Entry>(); } } public class Entry { [XmlElement(ElementName = "title")] public string Title { get; set; } [XmlElement(ElementName = "updated")] public DateTime? Updated { get; set; } [XmlElement(ElementName = "id")] public string Id { get; set; } [XmlElement(ElementName = "link")] public Link Link { get; set; } [XmlElement(ElementName = "summary")] public string Summary { get; set; } } public class Link { [XmlAttribute(AttributeName = "href")] public string Href { get; set; } } 

Here is a working example for writing / reading a feed:

 class Program { static void Main(string[] args) { Feed feed = new Feed(); feed.Title = "Exmple Feed"; feed.Updated = DateTime.Now; feed.Link = new Link { Href = "http://example.org/" }; feed.Entries.Add( new Entry { Title = "Atom-Powered Robots Run Amok", Link = new Link { Href = "http://example.org/2003/12/13/atom03" }, Updated = DateTime.Now, Summary = "Some text." }); XmlSerializer serializer = new XmlSerializer(typeof(Feed), "http://www.w3.org/2005/Atom"); using (StreamWriter sw = new StreamWriter("c:\\testatom.xml")) { serializer.Serialize(sw, feed); } using (StreamReader sr = new StreamReader("c:\\testatom.xml")) { Feed readFeed = serializer.Deserialize(sr) as Feed; } } } 
+2
source

A better starting point would be to use SyndicationFeed , which compares with the Atom 1.0 standard and RSS 2.0. Everything you need for a basic implementation should be available to you:

 XmlReader reader = XmlReader.Create("http://localhost/feeds/serializedFeed.xml"); SyndicationFeed feed = SyndicationFeed.Load(reader); // Feed title Console.WriteLine(feed.Title.Text); foreach(var item in feed.Items) { // Each item Console.WriteLine("Title: {0}", item.Title.Text); Console.WriteLine("Summary: {0}", ((TextSyndicationContent)item.Summary).Text); } 

Perhaps if you have any user requirements or you want to process RSS data differently than this standard, then Doug's answer will be a way.


Useful links:

0
source

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


All Articles