Chapter 19: XML

Chapter 19
  • Reading and Writing XML
  • Using the Document Object Model (DOM)
  • Using XML with Datasets
  • Using Visual C# .NET to Edit XML Files

Extensible Markup Language (XML) has become the unquestionable standard for generically marking data to be shared. As XML continues to grow in popularity, so too do the number of ways in which XML is being implemented. XML can be used for a variety of purposes, from obvious tasks such as marking up simple data files and storing temporary data to more complex tasks such as passing information from one program or process to another. This chapter describes methods for reading and writing XML files, working with XML documents, using XML with datasets, editing raw XML data, and creating a schema for an XML file.

The Microsoft .NET Framework, and thus Microsoft Visual C# .NET, provides wide support for the use of XML in an open, standards-compliant manner. A number of XML classes and namespaces are available. In general, you’ll find the classes you need in the System.Xml namespace. Within System.Xml are a number of other namespaces, including the following:

  • System.Xml.XPath

  • System.Xml.Schema

  • System.Xml.Xsl

Which of these namespaces you’ll want to include in your programs will depend on what you want to accomplish. For example, if you want to address parts of an XML document and manipulate strings and numbers, you should include the System.Xml.XPath namespace. System.Xml.Schema contains classes that enable you to perform schema mapping and validation. System.Xml.Xsl provides support for transforming XML-based data into HTML or other presentation formats. Most often you’ll simply include System.Xml.

These namespaces contain a number of classes, which can be broken into four categories, as follows:

  • Classes for performing basic XML parsing and writing operations

  • Classes for editing XML in a document, node, or structure

  • Classes for validating XML

  • Classes for transforming XML

Part III: Programming Windows Forms