<?xml version="1.0"?

A Windows Forms application reads the following XML file.

<?xml version=”1.0″?>
<x:catalog xmlns:x=”urn:books”>
<book id=”bk101″>
<author>Gambardella, Matthew</author>
<title>XML Developer’s Guide</title>
</book>
<book id=”bk102″>
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
</book>
</x:catalog>

The form initialization loads this file into an XmlDocument object named docBooks. You need to populate a ListBox control named lstBooks with the concatenated book ID and title of each book.
Which code segment should you use?

A Windows Forms application reads the following XML file.

<?xml version=”1.0″?>
<x:catalog xmlns:x=”urn:books”>
<book id=”bk101″>
<author>Gambardella, Matthew</author>
<title>XML Developer’s Guide</title>
</book>
<book id=”bk102″>
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
</book>
</x:catalog>

The form initialization loads this file into an XmlDocument object named docBooks. You need to populate a ListBox control named lstBooks with the concatenated book ID and title of each book.
Which code segment should you use?

A.
XmlNodeList elements = docBooks.GetElementsByTagName(“book”);
foreach (XmlElement node in elements) {
string s = node.GetAttribute(“id”) + ” – “;
s += node.SelectSingleNode(“title”).InnerText;
lstBooks.Items.Add(s);
}

B.
XmlNodeList elements = docBooks.GetElementsByTagName(“book”);
foreach (XmlElement node in elements) {
string s = node.SelectSingleNode(“id”) + ” – “;
s += node.GetAttribute(“title”);
lstBooks.Items.Add(s);
}

C.
XmlNodeList elements = docBooks.GetElementsByTagName(“book”);
foreach (XmlElement node in elements) {
string s = node.GetAttribute(“id”) + ” – “;
s += node.SelectSingleNode(“title”).Value;
lstBooks.Items.Add(s);
}

D.
XmlNodeList elements = docBooks.GetElementsByTagName(“book”);
foreach (XmlElement node in elements) {
lstBooks.Items.Add(node.InnerXml);
}



Leave a Reply 0

Your email address will not be published. Required fields are marked *