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>Ralis, 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>Ralis, 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.SelectStingleNode("title").InnerText;
1stBook.Items.Add(s);
}

B.
XmlNodeList elements=docBooks.GetElementsByTagName("Book");
foreach (XmlElement node in elements) {
string s = node.SelectSingleNode("id") + "-";
s += node.GetAttribute("title");
1stBook.Items.Add(s);
}

C.
XmlNodeList elements=docBooks.GetElementsByTagName("Book");
foreach (XmlElement node in elements) {
string s = node.GetAttribute("id") + "-";
s += node.SelectSingleNode("title");.Value
1stBook.Items.Add(s);
}

D.
XmlNodeList elements=docBooks.GetElementsByTagName("Book");
foreach (XmlElement node in elements) {
1stBook.Items.Add(node.InnerXml);
}



Leave a Reply 0

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