You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 to develop an application. A file named books.xml contains the following XML.
<bib>
<book title=”Programming in Unix” year=”1992″>
<author>Author1</author>
<author>Author2</author>
<author> Author 3 </author>
</book>
</bib>
The application must generate an XML result that contains an XML element named BookTitle for each book. The text content of the element must contain the title of the book.
You need to create a query that generates the new XML result. What should you do?
A.
XDocument document = XDocument.Load(“books.xml”);
var query = from node in document.Descendants()
where node.Name.LocalName == “book”
select new XElement(“BookTitle”, node.FirstAttribute.Value);
B.
XDocument document = XDocument.Load(“books.xml”);
var query = from node in document.DescendantNodes()
where node.ToString() == “book”
select new XText(“BookTitle” + node.ToString());
C.
XDocument document = XDocument.Load(“books.xml”);
var query = from node in document.Descendants()
where node.Name.LocalName == “book”
select new XElement(“BookTitle”).Value = node.FirstAttribute.Value;
D.
XDocument document = XDocument.Load(“books.xml”);
var query = from node in document.DescendantNodes()
where node.ToString() == “book”
select new XElement(“BookTitle”, node.ToString());
Considering that attributes can be specified in any order, it is a bug to assume that node.FirstAttribute will return the title attribute. However, it is certainly more correct than using node.ToString().
node.ToString() gives “…”, not just the name of the tag.