You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
The application contains the following XML document:
<bib>
<book title=”TCP/IP Illusrated” year=”1994″>
<author>Author1</author>
</book>
<book title=”Programming in UNIX” year=”1992″>
<author>Author1</author>
<author>Author2</author>
<author>Author3</author>
</book>
<book title=”Data on the web” year=”2000″>
<author>Author4</author>
<author>Author3</author>
</book>
</bib>
You add the following code fragment. (Line numbers are included for reference only.)
01 public IEnumerable<XElement> GetBooks(string xml)
02 {
03 XDocument doc = XDocument.Parse(xml);
04 …
05 }
You need to return a list of book XML element that are authored by Author1. Which code segment should you insert at line 04?

A.
return doc.Element(“bib”).Elements()
.SelectMany(el => el.Elements()
.Where(e2 => e2.Equals(new XElement(“author”, “Author1”))));
B.
return doc.Element(“bib”).Elements()
.SelectMany(el => el.Elements()
.Where(e2 => (string)e2 == “Author1”));
C.
return doc.Elements(“bib”).Elements()
.Where(e1 => e1.Elements().Any(e2 => (string)e2 == “Author1”));
D.
return doc.Elements(“bib”).Elements()
.Where(e1 => e1.Elements().Any(e2 => e2.Equals(new XElement(“author”, “Author1”))));
C is the best answer, but it’s not the correct answer. It should in fact be:
.Where(e1 => e1.Elements(“author”).Any(e2 => (string)e2 == “Author1″));
Because if, for example, I add a book without an author, but with a musician named ‘Author1’, this book will alse be included in the list.
”
Author1
…
0
0
Verified answer C using Visual Studio. Also, Ed van Gagledonk is correct, answer C only looks at value of an element, and not element’s XName, so it won’t tell a difference between:
Author1
and
Author1
But by now we all know most of these questions are buggy as hell…
0
0
The forum filters out greater and less than signs. The previous comment should read:
[author]Author1[/author]
and
[musician]Author1[/musician]
(I replaced angle brackets with square brackets)
0
0