Select author name field from Atom feed using LINQ (C #)

I am trying to select the "name" field from an author node in an ATOM feed using LINQ. I can get all the fields that I need:

XDocument stories = XDocument.Parse(xmlContent);
XNamespace xmlns = "http://www.w3.org/2005/Atom";
var story = from entry in stories.Descendants(xmlns + "entry")
            select new Story
            {
                Title = entry.Element(xmlns + "title").Value,
                Content = entry.Element(xmlns + "content").Value
            };

How can I choose author name -> name in this script?

+5
source share
2 answers

Basically you want:

entry.Element(xmlns + "author").Element(xmlns + "name").Value

But you can wrap this with an additional method so that you can easily take appropriate measures if there are no elements of the author or name. You can also think about what you want if there is more than one author.

There may also be an element of the author in the feed ... you just need to keep in mind.

+5
source

It could be something like this:

        var story = from entry in stories.Descendants(xmlns + "entry")
                    from a in entry.Descendants(xmlns + "author")
                    select new Story
                    {
                        Title = entry.Element(xmlns + "title").Value,
                        Content = entry.Element(xmlns + "subtitle").Value,
                        Author = new AuthorInfo(
                            a.Element(xmlns + "name").Value,
                            a.Element(xmlns + "email").Value,
                            a.Element(xmlns + "uri").Value
                         )
                    };
+3

All Articles