4

I don't know about this much at all, I have built-in page that contains a gridview to describe daily input data separated into columns from different authors, it looks like an excel file. And there is an Atom link at the bottom.

If I click on one row's link especially the author of the post, I will be directed to the author's property page in which there will be name, current work done, and how much he has written his book (50/70 80% called status etc), I wonder how can I read in this information and display it in another view of a related application; that is I know the feed's URL only, I really have no clue how that can be done. Thank you for any help.

Mackintoast
  • 1,397
  • 5
  • 17
  • 25
  • I'll actually be working on this today, too bad I haven't wrapped my head around it completely yet. I do know that I'll be using a DataContractto consume the XML and deliver it as an object. – Chase Florell Feb 17 '12 at 17:25
  • http://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed.aspx – SLaks Feb 17 '12 at 17:26
  • http://stackoverflow.com/questions/9329067/reading-wordpress-rss-with-c-sharp-content-different/ – L.B Feb 17 '12 at 17:28
  • Thanks, But please be sure that except the two links I know about the author properties sites I know nothing else especially the internal linkage between that site and the site I am working on, but in what I am doing, there is a small block of code referencing to that site addresses http://www.something.com/works/ and http://www.something.com/author?sortir%asc for example I will try it out with C# on next Monday and post back to see if it works :-D – Mackintoast Feb 17 '12 at 17:38

2 Answers2

3

I haven't tried to use the DataContractSerializer with specialized XML formats, but the XmlSerializer lets you set what is an attribute and what is an element. It is the easiest method as far as I am concerned because you can make a nice object model and use it to read any XML. Here is a PARTIAL example of reading an atom feed. You will need to perform an HttpWebRequest to get the XML (which is pretty straight forward) 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 sample to write/read the 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;
        }
    }
}
Doug Lampe
  • 1,426
  • 15
  • 8
0

The best starting point would be to use the SyndicationFeed, which maps to the Atom 1.0 and RSS 2.0 standard. 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 some custom requirements, or want to handle the RSS data differently than this standard, then Doug's answer would be the way to go.


Useful references:

JDandChips
  • 9,780
  • 3
  • 30
  • 46