Pass the MCAD/MCSD: Learning to Access and Manipulate XML Data
Objectives
This chapter covers the following Microsoft-specified objective for the "Consuming and Manipulating Data" section of the "Developing XML Web Services and Server Components with Microsoft Visual C# .NET and the Microsoft .NET Framework" exam:
Access and manipulate XML Data.
Access an XML file by using the Document Object Model (DOM) and an XmlReader.
Transform DataSet data into XML data.
Use XPath to query XML data.
Generate and use an XSD schema.
Write a SQL statement that retrieves XML data from a SQL Server database.
Update a SQL Server database by using XML.
Validate an XML document.
Extensible Markup Language (far better known as XML) is pervasive in .NET. It's used as the format for configuration files, as the transmission format for SOAP messages, and in many other places. It's also rapidly becoming the most widespread common language for many development platforms.
This objective tests your ability to perform many XML development tasks. To pass this section of the exam, you need to know how to read an XML file from disk, and how to create your own XML from a DataSet object in your application. You also need to be familiar with the XPath query language, and with the creation and use of XSD schema files.
You'll also need to understand the connections that Microsoft SQL Server has with the XML universe. You need to be able to extract SQL Server data in XML format, and to be able to update a SQL Server database by sending it properly formatted XML.
Finally, the exam tests your ability to validate XML to confirm that it conforms to a proper format. The .NET Framework includes several means of validating XML that you should be familiar with.
Outline
Introduction
Accessing an XML File
- Understanding the DOM
- Using an XMLReader Object
- The XmlNode Class
- The XmlDocument Class
Synchronizing DataSet Objects with XML
- The XmlDataDocument Class
- Synchronizing a DataSet Object with an XmlDataDocument Object
- Starting with an XmlDataDocument Object
- Starting with a Full DataSet Object
- Starting with an XML Schema
Understanding XPath
- The XPath Language
- Using the XPathNavigator Class
- Selecting Nodes with XPath
- Navigating Nodes with XPath
Generating and Using XSD Schemas
- Generating an XSD Schema
- Using an XSD Schema
- Validating Against XSD
- Validating Against a DTD
Using XML with SQL Server
- Generating XML with SQL Statements
- Understanding the FOR XML Clause
- Using ExecuteXmlReader() Method
- Updating SQL Data by Using XML
- Installing SQLXML
- Using DiffGrams
Chapter Summary
Apply Your Knowledge
-
Use the XmlDocument and XmlNode objects to navigate through some XML files. Inspect the node types that you find and understand how they relate to the original XML.
-
Use the XmlDataDocument class to synchronize a DataSet object with an XML file. Save the XML file to disk and inspect its contents. Understand how the generated XML relates to the original DataSet object.
-
Use an XPath processor to run XPath queries against an XML file. Make sure you know the XPath syntax to select portions of the XML.
-
Use the methods of the DataSet object to create XSD files. Inspect the generated XSD and understand how it relates to the original objects.
-
Use XML to read and write SQL Server data. You can install the MSDE version of SQL Server from your Visual Studio .NET CD-ROMs if you don't have a full SQL Server to work with.
-
Use the XmlValidatingReader class to validate an XML file. Make a change to the file that makes it invalid and examine the results when you try to validate the file.
-
Review the XML Data section of the Common Tasks QuickStart Tutorials that ship as part of the .NET Framework SDK.
Introduction
You can't use the .NET Framework effectively unless you're familiar with XML. That's true even if you're working only with desktop applications, but if you want to write XML Web Services and other distributed applications, XML knowledge is even more important. The .NET Framework uses XML for many purposes itself, but it also makes it very easy for you to use XML in your own applications.
The FCL's support for XML is mainly contained in the System.Xml namespace. This namespace contains objects to parse, validate, and manipulate XML. You can read and write XML, use XPath to navigate through an XML document, or check to see whether a particular document is valid XML by using the objects in this namespace.
NOTE
XML Basics In this chapter, I've assumed that you're already familiar with the basics of XML, such as elements and attributes. If you need a refresher course on XML Basics, refer to Appendix B, "XML Standards and Syntax."
As you're learning about XML, you'll become familiar with some other standards as well. These include the XPath query language and the XSD schema language. You'll see in this chapter how these other standards are integrated into the .NET Framework's XML support.