Home > Articles > Certification > Microsoft Certification

  • Print
  • + Share This
This chapter is from the book

Apply Your Knowledge

Exercises

2.1 - Compiling XPath Expressions

The .NET Framework developers invested a lot of effort in finding ways to make code more efficient. One performance enhancement in evaluating XPath expressions is the capability to precompile an expression for reuse. This exercise shows you how to use the Compile() method of the XPathNavigator class to create a precompiled XPath expression, represented as an XPathExpression object.

Estimated Time: 15 minutes.

  1. Create a new Visual C# .NET project to use for the exercises in this chapter.

  2. Add a new XML file to the project. Name the new file Books1.xml. Modify the XML for Books1.xml as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <Books>
      <Book Pages="1088">
        <Author>Delaney, Kalen</Author>
        <Title>
          Inside Microsoft SQL Server 2000
        </Title>
        <Publisher>Microsoft Press
        </Publisher>
      </Book>
      <Book Pages="997">
        <Author>Burton, Kevin</Author>
        <Title>.NET Common Language Runtime
        </Title>
        <Publisher>Sams</Publisher>
      </Book>
      <Book Pages="392">
        <Author>Cooper, James W.</Author>
        <Title>C# Design Patterns</Title>
        <Publisher>Addison Wesley
        </Publisher>
      </Book>
    </Books>
  3. Add a new XML file to the project. Name the new file Books2.xml. Modify the XML for Books2.xml as follows:

    <?xml version="1.0" encoding="UTF-8" ?>
    <Books>
      <Book Pages="792">
        <Author>LaMacchia, Brian A.
        </Author>
        <Title>.NET Framework Security
        </Title>
        <Publisher>Addison Wesley
        </Publisher>
      </Book>
      <Book Pages="383">
        <Author>Bischof Brian</Author>
        <Title>The .NET Languages:
         A Quick Translation Guide</Title>
        <Publisher>Apress</Publisher>
      </Book>
      <Book Pages="196">
        <Author>Simpson, John E.</Author>
        <Title>XPath and XPointer</Title>
        <Publisher>O'Reilly</Publisher>
      </Book>
    </Books>
  4. Add a new form to the project. Name the new form Exercise2_1.cs.

  5. Add a Label control, a TextBox control (txtXPath), a Button control (btnEvaluate), and a ListBox control (lbNodes) to the form.

  6. Switch to code view and add the following using directive:

  7. using System.Xml.XPath;

  8. Double-click the Button control and add code to handle the button's Click event:

    private void btnEvaluate_Click(
      object sender, System.EventArgs e)
    {
      // Load the Books1.xml file
      XPathDocument xpd1 =
        new XPathDocument(
        @"..\..\Books1.xml");
      // Get the associated navigator
      XPathNavigator xpn1 =
        xpd1.CreateNavigator();
      // Precompile an XPath expression
      XPathExpression xpe =
        xpn1.Compile(txtXPath.Text);
      // Retrieve nodes to match
      // the expression
      XPathNodeIterator xpni =
        xpn1.Select(xpe);
      // And dump the results
      lbNodes.Items.Clear();
      lbNodes.Items.Add(
        "Results from Books1.xml:");
      // Load the Books1.xml file
    
      while(xpni.MoveNext())
      {
        lbNodes.Items.Add(" " +
         xpni.Current.NodeType.ToString() +
         ": " + xpni.Current.Name + " = " +
         xpni.Current.Value);
      }
    
      // Now get the second document
      XPathDocument xpd2 =
        new XPathDocument(
        @"..\..\Books2.xml");
      // Get the associated navigator
      XPathNavigator xpn2 =
        xpd2.CreateNavigator();
      // Retrieve nodes to match
      // the expression
      // Reuse the precompiled expression
      xpni = xpn2.Select(xpe);
      // And dump the results
      lbNodes.Items.Add(
        "Results from Books2.xml:");
      while (xpni.MoveNext())
      {
        lbNodes.Items.Add(" " +
        xpni.Current.NodeType.ToString() +
        ": " + xpni.Current.Name + " = " +
          xpni.Current.Value);
      }
    }
  9. Insert the Main() method to launch the form. Set the form as the startup form for the project.

  10. Run the project. Enter an XPath expression and click the button. The code precompiles the expression and then uses the precompiled version to select nodes from each of the two XML files, as shown in Figure 2.18.

Figure 2.18Figure 2.18 The Compile() method of the XPathNavigator class allows you to create a precompiled XPath expression.

2.2 - Creating DiffGrams

If you'd like to experiment with the DiffGram format, you don't have to create DiffGrams by hand. You can use the WriteXml() method of the DataSet object to create a DiffGram containing all the changes made since the DataSet object was initialized. This exercise walks you through the process of creating a DiffGram.

Estimated Time: 15 minutes.

  1. Add a new form to the project. Name the new form Exercise2_2.cs.

  2. Add a DataGrid control (dgProducts) and a Button control (btnWriteDiffGram) to the form.

  3. Expand the Server Explorer tree view to show a Data Connection to the Northwind sample database. Drag and drop the connection to the form.

  4. Drag a SaveFileDialog component from the Toolbox and drop it on the form. Set the FileName property of the component to diffgram.xml.

  5. Switch to code view and add the following using directive:

    using System.Data;
    using System.Data.SqlClient;
  6. Add the following DataSet declaration to the class definition:

    DataSet dsProducts = new DataSet();
  7. Double-click the form and add the following code to handle the form's Load event:

    private void Exercise2_2_Load(
      object sender, System.EventArgs e)
    {
      // Create a command to retrieve data
      SqlCommand cmd =
        sqlConnection1.CreateCommand();
      cmd.CommandType = CommandType.Text;
      cmd.CommandText =
        "SELECT * FROM Products " +
        "WHERE CategoryID = 6";
      // Retrieve the data to the DataSet
      SqlDataAdapter da =
        new SqlDataAdapter();
      da.SelectCommand = cmd;
      da.Fill(dsProducts, "Products");
      // Bind it to the user interface
      dgProducts.DataSource = dsProducts;
      dgProducts.DataMember = "Products";
    }
  8. Double-click the button and add the following code to handle the button's Click event:

    private void btnWriteDiffGram_Click(
      object sender, System.EventArgs e)
    {
      // Prompt for a filename
      saveFileDialog1.ShowDialog();
      // Write out the DiffGram
      dsProducts.WriteXml(
        saveFileDialog1.FileName,
        XmlWriteMode.DiffGram);
    }
  9. Insert the Main() method to launch the form. Set the form as the startup form for the project.

  10. Run the project. The form displays records from the Northwind Products table for the selected category ID. Make some changes to the data; you can use the DataGrid to add, edit, or delete records. Click the button and select a name for the DiffGram. Click the Write DiffGram button to create the DiffGram. Open the new DiffGram in a text editor and inspect it to see how your changes are represented.

2.3 - Using XPath with Relational Data

By combining an XML-synchronized DataSet object with an XPath expression, you can use XPath to select information from a relational database. This exercise shows you how you can use a DataSet object, an XmlDataDocument object, and an XPathNavigator object together.

Estimated Time: 25 minutes.

  1. Add a new form to the project. Name the new form Exercise2_3.cs.

  2. Add two Label controls, two TextBox controls (txtSQLStatement and txtXPath), two Button controls (btnCreateDataSet and btnSelectNodes), and a ListBox control (lbNodes) to the form.

  3. Expand the Server Explorer tree view to show a data connection to the Northwind sample database. Drag and drop the connection to the form.

  4. Switch to code view and add the following using directives:

    using System.Data;
    using System.Data.SqlClient;
    using System.Xml;
    using System.Xml.XPath;
  5. Add the following code to the class definition:

    DataSet ds;
    XmlDataDocument xdd;
  6. Double-click the Create DataSet button and enter code to load a DataSet object and an XmlDataDocument object:

    private void btnCreateDataSet_Click(
      object sender, System.EventArgs e)
    {
      // Create a command to retrieve data
      SqlCommand cmd =
        sqlConnection1.CreateCommand();
      cmd.CommandType = CommandType.Text;
      cmd.CommandText = txtSQLStatement.Text;
      // Retrieve the data to the DataSet
      SqlDataAdapter da =
        new SqlDataAdapter();
      da.SelectCommand = cmd;
      ds = new DataSet();
      da.Fill(ds, "Table1");
      // Retrieve the XML form of the Dataset
      xdd = new XmlDataDocument(ds);
    }
  7. Double-click the Select Nodes button and enter code to evaluate the XPath expression:

    private void btnSelectNodes_Click(
      object sender, System.EventArgs e)
    {
      // Get the associated navigator
      XPathNavigator xpn =
        xdd.CreateNavigator();
      // Retrieve nodes to match
      // the expression
      XPathNodeIterator xpni =
        xpn.Select(txtXPath.Text);
      // And dump the results
      lbNodes.Items.Clear();
      while (xpni.MoveNext())
      {
        lbNodes.Items.Add(
          xpni.Current.NodeType.ToString()
          + ": " + xpni.Current.Name +
          " = " + xpni.Current.Value);
      }
    }
  8. Insert the Main() method to launch the form. Set the form as the startup form for the project.

  9. Run the project. Enter a SQL Statement that retrieves rows and click the first button. Then enter an XPath expression and click the second button. You'll see the XPath query results as a set of XML nodes, as shown in Figure 2.19.

Figure 2.19Figure 2.19 You can use XPath with an XmlDataDocument object.

Review Questions

  1. How are XML elements and attributes represented in the DOM?

  2. When should you use an XmlTextReader object by itself rather than with an XmlDocument object?

  3. Explain three ways to synchronize an XmlDataDocument object with a DataSet object.

  4. Why should you use an explicit path rather than a documentwide wildcard in an XPath expression?

  5. What is the difference between the XPath expressions /Customers/Customer/Order[1] and (/Customers/Customer/Order)[1]?

  6. How can you instantiate an XPathNavigator object?

  7. What options are there for validating an XML file with the XmlValidatingReader object?

  8. What are the three main variants of the FOR XML clause in T-SQL?

  9. How can you generate schema information with the FOR XML clause in T-SQL?

  10. What data operations can be represented in a DiffGram?

Exam Questions

  1. Your application contains an XML file, Orders.xml, with the following content:

    <?xml version="1.0" encoding="utf-8" ?>
    <Orders>
     <Order OrderID="1">
      <OrderDate>1/1/2003</OrderDate>
     </Order>
     <Order OrderID="2">
      <OrderDate>1/2/2003</OrderDate>
     </Order>
     <Order OrderID="3">
      <OrderDate>1/3/2003</OrderDate>
     </Order>
    </Orders>

    Your application also contains a form with a Button control named btnProcess and a ListBox control named lbNodes. The Click event handler for the Button control has this code:

    private void btnProcess_Click(
      object sender, System.EventArgs e)
    {
      XmlTextReader xtr = new
        XmlTextReader(@"..\..\Orders.xml");
      while(xtr.Read())
      {
        if ((xtr.NodeType ==
            XmlNodeType.Attribute)
          || (xtr.NodeType ==
            XmlNodeType.Element)
          || (xtr.NodeType ==
            XmlNodeType.Text))
        {
          if(xtr.HasValue)
           lbNodes.Items.Add(xtr.Value);
        }
      }
      xtr.Close();
    }

    What will be the contents of the ListBox control after you click the button?

    1. 1
      1/1/2003
      2
      1/2/2003
      3
      1/3/2003
    2. 1
      2
      3
    3. Orders
      Order
      1/1/2003
      Order
      1/2/2003
      Order
      1/3/2003
    4. 1/1/2003
      1/2/2003
      1/3/2003
  2. Your application contains the following XML file:

    <?xml version="1.0" encoding="utf-8" ?>
    <Orders>
     <Order OrderID="1">
      <OrderDate>1/1/2003</OrderDate>
     </Order>
     <Order OrderID="2">
      <OrderDate>1/2/2003</OrderDate>
     </Order>
     <Order OrderID="3">
      <OrderDate>1/3/2003</OrderDate>
     </Order>
    </Orders>

    Your application uses the ReadXmlSchema() method of the DataSet object to create a DataSet object with an appropriate schema for this XML file. Which of the following mappings between XML entities and DataSet object entities will this method create?

    1. Orders and Order will become DataTable objects. OrderID will become a DataColumn object. OrderDate will not be mapped.

    2. Orders and Order will become DataTable objects. OrderID and OrderDate will become DataColumn objects.

    3. Orders and Order will become DataTable objects. OrderDate will become a DataColumn. OrderID will not be mapped.

    4. Orders will become a DataTable. Order and OrderDate will become DataColumn objects.

  3. Your application includes an XML file that represents inventory in a warehouse. Each inventory item is identified by 50 different elements. You need to work with 4 of these elements on a DataGrid control. You plan to create a DataSet object containing the appropriate data that can be bound to the DataGrid control. How should you proceed?

    1. Load the XML file into an XmlDocument object. Create a DataSet object containing a single DataTable object with the desired column. Write code that loops through the nodes in the XmlDocument object and that transfers the data from the appropriate nodes to the DataSet object.

    2. Load the XML file into an XmlDataDocument object. Retrieve the DataSet object from the XmlDataDocument object's DataSet property.

    3. Load the XML file into a DataSet object by calling the DataSet object's ReadXml() method.

    4. Create a schema that includes the four required elements. Create a DataSet object from this schema. Create an XmlDataDocument object from the DataSet object. Load the XML document into the XmlDataDocument object.

  4. You use a SqlDataAdapter object to fill a DataSet object with the contents of the Customers table in your database. The CompanyName of the first customer is "Biggs Industries". You synchronize an XmlDataDocument object with the DataSet object. In the DataSet object, you change the CompanyName of the first customer to "Biggs Limited". After that, in the XmlDataDocument, you change the value of the corresponding node to "Biggs Co." When you call the Update() method of the SqlDataAdapter object, what is the effect?

    1. The CompanyName in the database remains "Biggs Industries".

    2. The CompanyName in the database is changed to "Biggs Limited".

    3. The CompanyName in the database is changed to "Biggs Co."

    4. A record locking error is thrown.

  5. Your application contains the following XML file:

    <?xml version="1.0" encoding="utf-8" ?>
    <Customers>
     <Customer>
      <CustomerName>A Company</CustomerName>
      <Orders>
       <Order OrderID="1">
        <OrderDate>1/1/2003</OrderDate>
       </Order>
       <Order OrderID="2">
        <OrderDate>1/2/2003</OrderDate>
       </Order>
      </Orders>
     </Customer>
     <Customer>
      <CustomerName>B Company</CustomerName>
      <Orders>
       <Order OrderID="3">
        <OrderDate>1/2/2003</OrderDate>
       </Order>
       <Order OrderID="4">
        <OrderDate>1/3/2003</OrderDate>
       </Order>
      </Orders>
     </Customer>
     <Customer>
      <CustomerName>C Company</CustomerName>
      <Orders>
       <Order OrderID="5">
        <OrderDate>1/4/2003</OrderDate>
       </Order>
       <Order OrderID="6">
        <OrderDate>1/5/2003</OrderDate>
       </Order>
      </Orders>
     </Customer>
    </Customers>

    Which XPath expression will return the first OrderID for each customer?

    1. /Customers/Customer/Orders/Order/@OrderID[1]

    2. (/Customers/Customer/Orders/Order)[1]/@OrderID

    3. /Customers/Customer/Orders/Order[1]/@OrderID

    4. (/Customers/Customer/Orders/Order/@OrderID)[1]

  6. Your application contains the following XML file:

    <?xml version="1.0" encoding="utf-8" ?>
    <Customers>
     <Customer>
      <CustomerName>A Company</CustomerName>
      <Orders>
       <Order OrderID="1">
        <OrderDate>1/1/2003</OrderDate>
       </Order>
       <Order OrderID="2">
        <OrderDate>1/2/2003</OrderDate>
       </Order>
      </Orders>
     </Customer>
     <Customer>
      <CustomerName>B Company</CustomerName>
      <Orders>
       <Order OrderID="3">
        <OrderDate>1/2/2003</OrderDate>
       </Order>
       <Order OrderID="4">
        <OrderDate>1/3/2003</OrderDate>
       </Order>
      </Orders>
     </Customer>
     <Customer>
      <CustomerName>C Company</CustomerName>
      <Orders>
       <Order OrderID="5">
        <OrderDate>1/4/2003</OrderDate>
       </Order>
       <Order OrderID="6">
        <OrderDate>1/5/2003</OrderDate>
       </Order>
      </Orders>
     </Customer>
    </Customers>

    Which XPath expression will return the CustomerName for all customers who placed an order on 1/3/2003?

    1. /Customers/Customer[./Orders/Order/OrderDate="1/3/2003"]/CustomerName

    2. /Customers/Customer[/Orders/Order/OrderDate="1/3/2003"]/CustomerName

    3. /Customers/Customer[//Orders/Order/OrderDate="1/3/2003"]/CustomerName

    4. /Customers/Customer[Orders/Order/OrderDate="1/3/2003"]/CustomerName

  7. Your application allows the user to perform arbitrary XPath queries on an XML document. The user does not need to be able to alter the document. Which approach will give you the maximum performance for these requirements?

    1. Read the document into an XmlDocument object. Use the CreateNavigator() method of the XmlDocument object to return an XPathNavigator object. Perform your queries by using the XPathNavigator object.

    2. Read the document into an XmlDataDocument object. Use the DataSet property of the XmlDataDocument object to return a DataSet object. Perform your queries by using the DataSet object.

    3. Read the document into an XmlDataDocument object. Use the CreateNavigator() method of the XmlDataDocument object to return an XPathNavigator object. Perform your queries by using the XPathNavigator object.

    4. Read the document into an XPathDocument object. Use the CreateNavigator() method of the XPathDocument object to return an XPathNavigator object. Perform your queries by using the XPathNavigator object.

  8. You are designing an application that will enable the user to explore the structure of an XML file. You need to allow the user to move to the parent node, next node, or first child node from the current node. Which object should you use to implement this requirement?

    1. XPathNavigator

    2. XmlReader

    3. XmlTextReader

    4. XPathExpression

  9. Your XML document has an inline schema. What is the minimum number of errors that this document will generate if you validate it with the XmlValidatingReader class?

    1. 0

    2. 1

    3. 2

    4. 3

  10. You are retrieving customer data from a SQL Server database into an XML document. You want the CustomerName and ContactName columns to be translated into XML elements. Which clause should you use in your SQL statement?

    1. FOR XML AUTO

    2. FOR XML RAW

    3. FOR XML EXPLICIT

    4. FOR XML AUTO, XMLDATA

  11. Your application contains the following code:

    private void btnReadXML_Click(
      object sender, System.EventArgs e)
    {
      // Create a command to retrieve XML
      SqlCommand sc =
        SqlConnection1.CreateCommand();
      sc.CommandType = SqlCommandType.Text;
      sc.CommandText =
       "SELECT Customers.CustomerID, " +
       "Customers.CompanyName," +
       "Orders.OrderID, Orders.OrderDate " +
       "FROM Customers INNER JOIN Orders " +
       "ON Customers.CustomerID = " +
       "Orders.CustomerID "
       + "WHERE Country = 'Brazil' AND " +
       "OrderDate BETWEEN '1997-03-15' " +
       "AND '1997-04-15' " +
       "FOR XML AUTO, ELEMENTS";
      // Read the XML into an XmlReader
      XmlReader xr = sc.ExecuteXmlReader();
      XmlDocument xd = new XmlDocument();
      xd.Load(xr);
    }

    When you run this code, you receive an error on the line of code that attempts to load the XmlDocument. What can you do to fix the problem?

    1. Use FOR XML RAW instead of FOR XML AUTO in the SQL statement.

    2. Replace the XmlDocument object with an XmlDataDocument object.

    3. Replace the SqlCommand object with a SqlXmlCommand object.

    4. Replace the XmlReader object with an XmlTextReader object.

  12. Your application contains the following code, which uses the SQLXML managed classes to apply a DiffGram to a SQL Server database:

    private void btnUpdate_Click(
      object sender, System.EventArgs e)
    {
      // Connect to the SQL Server database
      SqlXmlCommand sxc =
       new SqlXmlCommand(
       "Provider=SQLOLEDB;" +
       "Server=(local);database=Northwind;" +
       "Integrated Security=SSPI");
    
      // Set up the DiffGram
      sxc.CommandType =
        SqlXmlCommandType.DiffGram;
      FileStream fs =
       new FileStream(@"..\..\diffgram.xml",
        FileMode.Open);
      sxc.CommandStream = fs;
      // And execute it
      sxc.ExecuteNonQuery();
      MessageBox.Show(
        "Database was updated!");
    } 

    When you run the code, it does not update the database. The DiffGram is properly formatted. What should you do to correct this problem?

    1. Use a SqlCommand object in place of the SqlXmlCommand object.

    2. Supply an appropriate schema mapping file for the DiffGram.

    3. Store the text of the DiffGram in the CommandText property of the SqlXmlCommand object.

    4. Use a SqlConnection object to make the initial connection to the database.

  13. Which of these operations can be carried out in a SQL Server database by sending a properly-formatted DiffGram to the database? (Select two.)

    1. Adding a row to a table

    2. Adding a primary key to a table

    3. Deleting a row from a table

    4. Changing the data type of a column

  14. You are developing code that uses the XPathNavigator object to navigate among the nodes in the DOM representation of an XML document. The current node of the XPathNavigator is an element in the XML document that does not have any attributes or any children. You call the MoveToFirstChild() method of the XPathNavigator object. What is the result?

    1. The current node remains unchanged and there is no error.

    2. The current node remains unchanged and a runtime error is thrown.

    3. The next sibling of the current node becomes the current node and there is no error.

    4. The next sibling of the current node becomes the current node and a runtime error is thrown.

  15. Which of these operations requires you to have an XML schema file?

    1. Updating a SQL Server database with a DiffGram through the SQLXML Managed classes

    2. Validating an XML file with the XmlValidatingReader class

    3. Performing an XPath query with the XPathNavigator class

    4. Reading an XML file with the XmlTextReader class

Answers to Review Questions

  1. XML elements are represented as nodes within the DOM. XML attributes are represented as properties of their parent nodes.

  2. The XmlTextReader object provides forward-only, read-only access to XML data. For random access or for read-write access you should use the XmlDocument class or one of its derived classes.

  3. You can synchronize an XmlDataDocument object and a DataSet object by creating a DataSet object from an XmlDataDocument object, by creating an XmlDataDocument object from a DataSet object, or by creating both a DataSet object and an XmlDataDocument object from a schema.

  4. XPath expressions containing an explicit path, such as /Customers/Customer/Order/OrderID, are faster to evaluate than documentwide wildcard expressions such as //OrderID.

  5. /Customers/Customer/Order[1] selects the first order for each customer, whereas (/Customers/Customer/Order)[1] selects the first order in the entire document.

  6. You can instantiate an XPathNavigator object by calling the CreateNavigator() method of the XmlDocument, XmlDataDocument, or XPathDocument classes.

  7. You can use an XmlValidatingReader object to validate an XML file for conformance with an embedded schema, an XSD file, a DTD file, or an XDR file.

  8. The three main variants of FOR XML are FOR XML RAW, FOR XML AUTO, and FOR XML EXPLICIT.

  9. To include schema information with a FOR XML query, specify the XMLDATA option.

  10. DiffGrams can represent insertions, deletions, and modifications of the data in a DataSet object or SQL Server database.

Answers to Exam Questions

  1. D. When you read XML data with the help of XmlReader or its derived classes, nodes are not included for XML attributes, but only for XML elements and the text that they contain. XML element nodes do not have a value. The only text that will be printed out is the value of the text nodes within the OrderDate elements. For more information, see the section "Accessing an XML File" in this chapter.

  2. B. The ReadXmlSchema() method maps nested elements in the XML file to related DataTable objects in the DataSet object. At the leaf level of the DOM tree, both elements and attributes are mapped to DataColumn objects. For more information, see the section "Synchronizing DataSet Objects with XML" in this chapter.

  3. D. Looping through all the nodes in an XmlDocument object is comparatively slow. If you start with the full XML document or by calling the ReadXml() method of the DataSet object, the DataSet object will contain all 50 elements. Using a schema file enables you to limit the DataSet object to holding only the desired data. For more information, see the section "Synchronizing DataSet Objects with XML" in this chapter.

  4. C. The DataSet and the XmlDataDocument objects represent two different views of the same data structure. The last change made to either view is the change that is written back to the database. For more information, see the section "Synchronizing DataSet Objects with XML" in this chapter.

  5. C. /Customers/Customer/Orders/Order/@OrderID[1] selects the first OrderID for each order. (/Customers/Customer/Orders/Order)[1]/ @OrderID selects the OrderID for the first order in the entire file. (/Customers/Customer/Orders/Order/@OrderID)[1] selects the first OrderID in the entire file. The remaining choice is the correct one. For more information, see the section "Understanding XPath" in this chapter.

  6. A. The filtering expression needs to start with the ./ operator to indicate that it is filtering nodes under the current node at that point in the expression. For more information, see the section "Understanding XPath" in this chapter.

  7. D. The XPathDocument class is optimized for read-only XPath queries. For more information, see the section "Understanding XPath" in this chapter.

  8. A. The XPathNavigator object provides random access movement within the DOM. The XmlReader and XmlTextReader objects provide forward-only movement. The XPathExpression object is useful for retrieving a set of nodes, but not for navigating between nodes. For more information, see the section "Using the XPathNavigator Class" in this chapter.

  9. B. An inline schema cannot contain validation information for the root node of the document, so XML documents with inline schemas will always have at least one validation error. For more information, see the section "Using an XSD Schema" in this chapter.

  10. C. The raw and auto modes of the FOR XML statement always map columns to attributes, if the ELEMENTS option is not added in the FOR XML clause. Only explicit mode can map a column to an element. For more information, see the section "Understanding the FOR XML Clause" in this chapter.

  11. C. The SqlCommand.ExecuteXmlReader() method returns an XML fragment rather than an XML document. To load a complete and valid XML document, you need to use the SqlXmlCommand object (from the SQLXML Managed Classes) instead. For more information, see the section "Updating SQL Data by Using XML" in this chapter.

  12. B. When executing a DiffGram via the SQLXML managed classes, you must supply a mapping schema file. Otherwise, the code doesn't know which elements in the DiffGram map to which columns in the database. For more information, see the section "Using DiffGrams" in this chapter.

  13. A, C. DiffGrams are useful for performing data manipulation operations, but not for data definition operations. For more information, see the section "Using DiffGrams" in this chapter.

  14. A. The MoveTo methods of the XPathNavigator object always execute without error. If the requested move cannot be performed, the current node remains unchanged and the method returns false. For more information, see the section "Using the XPathNavigator Class" in this chapter.

  15. A. You cannot perform a DiffGram update without a schema file that specifies the mapping between XML elements and database columns. Validation can be performed with a DTD or XDR file instead of a schema. XPath queries and reading XML files do not require a schema. For more information, see the section "Using DiffGrams" in this chapter.

Suggested Readings and Resources

  1. Visual Studio .NET Combined Help Collection

    • Employing XML in the .NET Framework

    • XML and the DataSet

  2. Microsoft SQL Server Books Online

    • Retrieving XML Documents Using FOR XML

    • SELECT Statement

  3. .NET Framework QuickStart Tutorials, Common Tasks QuickStart, XML section.

  4. Mike Gunderloy. ADO and ADO.NET Programming. Sybex, 2002.

  5. John E. Simpson. XPath and XPointer. O'Reilly, 2002.

  6. John Griffin. XML and SQL Server. New Riders, 2001.

  • + Share This
  • 🔖 Save To Your Account