Exam Prep Questions
Question 1
Your SQL Server database contains employee timesheet information. You want to retrieve some of this information into an XML document. You decide to use the FOR XML T-SQL statement to retrieve the information. Your application requires the EmployeeName and EmployeeNumber database columns to be presented as XML elements. Which clause can you use in your SQL statement?
-
FOR XML AUTO
-
FOR XML RAW
-
FOR XML EXPLICIT
-
FOR XML AUTO, XMLDATA
Answer C is correct. FOR XML EXPLICIT maps columns to elements. Answers A and D are incorrect because the AUTO mode of the FOR XML clause maps columns to attributes rather than elements, unless you include the ELEMENTS modifier. Answer B is incorrect because the RAW mode of the FOR XML clause also maps columns to attributes. The EXPLICIT mode can map columns to elements.
Question 2
You have loaded an XML file that represents orders from a trading partner and are using an XPathNavigator class to navigate through the file. The current node of the XPathNavigator is an <Order> node that is emptythat is, this node does not contain any child nodes. Your code calls the MoveFirstChild method of the XPathNavigator object. What is the result?
-
No error occurs. The <Order> node remains the current node.
-
No error occurs. The root node of the document becomes the current node.
-
A runtime error is thrown. The root node of the document becomes the current node.
-
A runtime error is thrown. The <Order> node remains the current node.
Answer A is correct. The XPathNavigator ignores attempts to move to a nonexistent node. Answers B and C are incorrect because the current node remains unchanged when you try to move to a nonexistent node. Answers C and D are incorrect because no error is thrown when you try to move to a nonexistent node.
Question 3
Which of the following operations requires you to have an XML schema file?
-
Updating a SQL Server database by sending a DiffGram through the SQLXML managed classes
-
Writing an XML file to disk with the XmlTextWriter class
-
Validating an XML file with the XmlValidatingReader class
-
Performing an XPath query with the XPathNavigator class
Answer A is correct. The SQLXML managed classes require a schema file to match the DiffGram information to columns in the database. Answer B is incorrect because writing an XML file does not require a schema. Answer C is incorrect because you can validate an XML file against non-schema information such as a DTD. Answer D is incorrect because the XPathNavigator class does not use schema information to perform queries.
Question 4
You have an XML file with the following structure:
<?xml version="1.0" encoding="utf-8" ?> <Orders> <Order> <Product>...</Product> <Product>...</Product> ... <Order> <Product>...</Product> <Product>...</Product> ... ... </Orders>
You want to retrieve all the <Product> nodes from this file with the best performance. How should you proceed?
-
Read the XML file into an XmlDataDocument object. Retrieve a DataSet object from the DataSet property of the XmlDataDocument object. Create a DataView object within the DataSet object to return the required nodes.
-
Read the XML file into an XPathDocument object. Use the CreateNavigator method of the XPathDocument object to return an XPathNavigator object. Use an XPath expression with the Select method of the XPathNavigator object to return the required nodes.
-
Read the XML file into an XmlDocument object. Use the CreateNavigator method of the XmlDocument object to return an XPathNavigator object. Use an XPath expression with the Select method of the XPathNavigator object to return the required nodes.
-
Read the XML file into an XPathDocument object. Use the CreateNavigator method of the XPathDocument object to return an XPathNavigator object. Use the Move methods of the XPathNavigator object to move through the document, accumulating the required nodes as you go.
Answer B is correct. The XPathDocument class is optimized to perform XPath operations quickly. Answer A is incorrect because the DataSet object adds unnecessary overhead in this scenario. Answer C is incorrect because the XmlDocument object is not designed to support fast XPath queries. Answer D is incorrect because querying for the required nodes is faster than visiting all the nodes.
Question 5
Your application contains an XML file, Employees.xml, with the following content:
<?xml version="1.0" encoding="utf-8" ?> <Employees> <Employee EmployeeID="4"> <EmployeeName>John Farrel</EmployeeName> </Employee> <Employee EmployeeID="7"> <EmployeeName>Melanie Runnion</EmployeeName> </Employee> </Employees>
Your application also contains a form with a Button control named btnProcess and a ListBox control named lbNodes. The event handler for the Button control has this code:
Private Sub btnProcess_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnProcess.Click Dim xtr As XmlTextReader = _ New XmlTextReader("Employees.xml") Do While xtr.Read If (xtr.NodeType = _ XmlNodeType.Attribute) _ Or (xtr.NodeType = _ XmlNodeType.Element) _ Or (xtr.NodeType = _ XmlNodeType.Text) Then If xtr.HasValue Then lbnodes.Items.Add( _ xtr.Value) End If End If Loop xtr.Close() End Sub
What will be the contents of the ListBox control after you click the Button control?
-
4 7
-
4 John Farrel 7 Melanie Runnion
-
John Farrel Melanie Runnion
-
Employees Employee 4 EmployeeName John Farrel Employee 7 EmployeeName Melanie Runnion
Answer C is correct. The Value property refers only to the actual data within XML elements. Answers A and B are incorrect because XML attributes are not treated as nodes within the DOM. Answer D is incorrect because it includes element names, which are not treated as values.
Question 6
Your application loads an XML file selected by the user and then allows him to move through the XML file. You need to allow the user to move up to the parent node, over to the next node, or down to the first child node, by selecting choices on the user interface. Which object can you use to implement this requirement?
-
XPathNavigator
-
XmlReader
-
XPathExpression
-
XmlDataDocument
Answer A is correct. The XPathNavigator class provides random navigation through an XML document. Answer B is incorrect because the XmlReader class provides forward-only access to the XML document. Answer C is incorrect because the XPathExpression class represents a single XPath expression, not an XML document. Answer D is incorrect because the XmlDataDocument class is used to synchronize an XML document and a DataSet but does not provide random access to the XML document.
Question 7
You load an XML file of employee information into an XmlDataDocument object and then retrieve the DataSet from that object. The name of the first employee as stored in the XML file is "James Doe." In the DataSet, you change the value of the EmployeeName column of the first employee to "John Doe." After that, in the XmlDataDocument object, you change the value of the corresponding Node to "James Doe Jr." Finally, you call the AcceptChanges method of the DataSet object. What is the value of the first EmployeeName in the XmlDataDocument after taking these actions?
-
James Doe.
-
John Doe.
-
James Doe Jr.
-
The value is undefined because an error occurs.
Answer C is correct. The XmlDataDocument object and its corresponding DataSet object present two views of the exact same data, so changes to either one affect the other. Answer A is incorrect because the original value has been changed twice. Answer B is incorrect because the DataSet change gets overwritten by the XmlDataDocument change. Answer D is incorrect because you can make as many changes as you like without throwing an error.
Question 8
One of your company's suppliers has delivered a new parts list to you in the form of an XML file without an inline schema. You'd like to create an XML schema file that corresponds to this XML file. How can you do this with the least effort?
-
Import the XML file to a SQL Server database and then use drag and drop from the Server Explorer to the schema designer within Visual Studio .NET.
-
Use the schema designer within Visual Studio .NET to create the schema file by dragging and dropping elements, attributes, keys, and relations.
-
Use the DataSet.ReadXmlSchema method to create the schema information from the XML file.
-
Use the DataSet.InferXmlSchema method to create the schema information from the XML file.
Answer D is correct. The InferXmlSchema method can create a schema file based on the elements and attributes within an XML file. Answers A and B are incorrect because building a schema for an existing file within Visual Studio .NET is more work than letting the DataSet class do it for you. Answer C is incorrect because the ReadXmlSchema method requires embedded schema information.
Question 9
Which of these operations can you perform by sending a DiffGram to SQL Server? [Select all correct answers.]
-
Add new data to a table
-
Delete existing data from a table
-
Execute a stored procedure
-
Update the index on a table
Answers A and B are correct. The DiffGram format is designed to carry information on data changes. You can add new data, delete existing data, or modify existing data with a DiffGram. Answers C and D are incorrect because the DiffGram format doesn't carry any information on stored procedures or indexes.