Beginning XML with C# 2008 focuses on XML and how it is used within .NET 3.5. As you'd expect of a modern application framework, .NET 3.5 has extensive support for XML in everything from data access to configuration, from raw parsing to code documentation. This book demystifies all of this. It explains the basics of XML as well as the namespaces and objects you need to know in order to work efficiently with XML. You will see clear, practical examples that illustrate best practices in action. With this book, you'll learn everything you need to know from the basics of reading and writing XML data to using the DOM, from LINQ and SQL Server integration to SOAP and web services.
Author(s): Bipin Joshi
Edition: 2
Publisher: Apress
Year: 2018
Contents
About the Author
About the Technical Reviewer
Introduction
Chapter 1: Introducing XML and the .NET Framework
What Is XML?
Benefits of XML
XML Is an Industry Standard
XML Is Self-Describing
XML Is Extensible
XML Can Be Processed Easily
XML Can Be Used to Easily Exchange Data
XML Can Be Used to Easily Share Data
XML Can Be Used to Create Specialized Vocabularies
XML-Driven Applications
Rules of XML Grammar
Markup Is Case Sensitive
A Document Must Have One and Only One Root Element
A Start Tag Must Have an End Tag
Start and End Tags Must Be Properly Nested
Attribute Values Must Be Enclosed in Quotes
DTD and XML Schema
Parsing XML Documents
XSLT
XPath
The .NET Framework
.NET Framework and XML
Assemblies and Namespaces
System.Xml Namespace
System.Xml.Schema Namespace
System.Xml.XPath Namespace
System.Xml.Xsl Namespace
System.Xml.Serialization Namespace
System.Xml.Linq Namespace
The Classic XML Parsing Model of the .NET Framework
The LINQ-Based Parsing Model of the .NET Framework
.NET Configuration Files
ADO.NET
ASP.NET Web Forms Server Controls
XML Serialization
Web Services, WCF Services, and Web API
XML Documentation
XAML Markup
SQL Server XML Features
Working with Visual Studio
Creating Windows Forms Applications
Creating Class Libraries
Summary
Chapter 2: Manipulating XML Documents Using the Document Object Model
Using the DOM Parser
Knowing When to Use DOM
A Sample XML Document
Opening an Existing XML Document for Parsing
Navigating Through an XML Document
Looking for Specific Elements and Nodes
Retrieving Specific Elements Using the GetElementsByTagName() Method
Retrieving Specific Elements Using the GetElementById() Method
Selecting Specific Nodes Using the SelectNodes() Method
Selecting a Single Specific Node Using the SelectSingleNode() Method
Modifying XML Documents
Navigating Between Various Nodes
Modifying Existing Content
Deleting Existing Content
Adding New Content
Using Helper Methods
Dealing with Whitespace
Dealing with Namespaces
Understanding Events of the XmlDocument Class
Summary
Chapter 3: Reading and Writing XML Documents
What Are XML Readers and Writers?
When to Use Readers and Writers
Reader Classes
The XmlTextReader Class
The XmlValidatingReader Class
The XmlNodeReader Class
Reading XML Documents Using XmlTextReader
Opening XML Documents
Reading Attributes, Elements, and Values
Improving Performance by Using Name Tables
Dealing with Namespaces
Moving Between Elements
The ReadSubTree() Method
The ReadToDescendant() Method
The ReadToFollowing() Method
The ReadToNextSibling() Method
The Skip() Method
Moving Between Attributes
Reading Content
The ReadInnerXml() Method
The ReadOuterXml() Method
The ReadString() Method
Writing XML Documents Using XmlTextWriter
Exporting Columns As Elements
Exporting Columns As Attributes
Specifying Character Encoding
Formatting the Output
Including Namespace Support
Dealing with Nontextual Data
Serializing Data
Deserializing Data
Summary
Chapter 4: Accessing XML Documents Using the XPath Data Model
Overview of XPath
Location Path
Axis
Node Tests
Predicates
Putting It All Together
XPath Functions
The XPath Data Model
Creating XPathNavigator
Navigating an XML Document Using XPathNavigator
Selecting Nodes
Selecting Single Nodes
Selecting Children, Ancestors, and Descendants
Compiling XPath Expressions
Navigating Between Attributes
Retrieving Inner and Outer XML
Getting an XmlReader from XPathNavigator
Getting an XmlWriter from XPathNavigator
Editing XML Documents with the XPathNavigator Class
Adding Nodes
Modifying Nodes
Deleting Nodes
Saving Changes
Summary
Chapter 5: Validating XML Documents
Providing Structure for XML Documents
Document Type Definition (DTD)
XML Data Reduced (XDR) Schema
XML Schema Definition (XSD) Schema
Creating Structure for an XML Document
The Structure of Employees.xml
Creating the DTD
Creating the XML Schema
XSD for Employees.xml
Creating the XSD in Visual Studio Designer
Creating the Schema from XML Document Using Visual Studio IDE
Creating the Schema by Using the XML Schema Definition Tool
Creating the Schema from an XML Document
Creating the Schema from an Assembly
Creating Schemas by Using the Schema Object Model (SOM)
The Core SOM Classes
Creating an XML Schema Using the SOM
Creating the Schema
Creating a Simple Type for Names
Creating a Simple Type for Phone Numbers
Creating a Simple Type for Notes
Creating a Complex Type That Represents an Employee
Creating a Top-Level Complex Type
Creating the Root Element
Compiling the Schema
Saving the Schema
Attaching the DTD and XML Schemas to XML Documents
Inline DTDs
External DTDs
Inline XML Schema
External XML Schema
Adding Frequently Used Schemas to the Schema Cache
Using XmlReader to Validate XML Documents
Using XmlDocument to Validate XML Documents
Using XPathNavigator to Validate XML Documents
Specifying XML Schema via Code
Summary
Chapter 6: Transforming XML with XSLT
Overview of XSLT
Applying Templates Using
Branching Using
Branching Using and
Transforming Elements and Attributes
The XslCompiledTransform Class
Performing Transformations Using XslCompiledTransform
Passing Arguments to a Transformation
Using Script Blocks in an XSLT Style Sheet
Using Extension Objects
Compiling XSLT Style Sheets
Summary
Chapter 7: XML in ADO.NET
Overview of ADO.NET Architecture
Connected Data Access
Disconnected Data Access
ADO.NET Data Providers
SQL Server Data Provider
OLEDB Data Provider
ODBC Data Provider
Oracle Data Provider
The Assemblies and Namespaces Involved
ADO.NET Classes
Connection (SqlConnection and OleDbConnection)
Command (SqlCommand and OleDbCommand)
Parameter (SqlParameter and OleDbParameter)
DataReader (SqlDataReader and OleDbDataReader)
DataAdapter (SqlDataAdapter and OleDbDataAdapter)
DataSet
XML and Connected Data Access
Using the ExecuteXmlReader() Method
XML and Disconnected Data Access
Understanding DataSet
Understanding DataAdapter
Working with DataSet and DataAdapter
Filling a DataSet
Accessing Data from DataSet
Adding New Rows
Updating an Existing Row
Deleting a Row
Using DataRow States
Saving the Changes to the Database
Saving DataSet Contents As XML
Saving Only the Schema
Extracting DataSet Contents As an XML String
Reading XML Data into DataSet
Using the Automatic Read Operation
Reading DiffGrams
Reading XML Fragments
Ignoring Schema Information
Inferring Schema Information
Reading Schema Information
Generating Menus Dynamically Based On an XML File
Reading Only the Schema Information
Creating a Typed DataSet
Using Visual Studio to Create a Typed DataSet
Using the xsd.exe Tool to Create a Typed DataSet
Summary
Chapter 8: XML Serialization
Understanding the Flavors of Serialization
Classes Involved in the XML Serialization
Serializing and Deserializing Objects Using XmlSerializer
Handling Events Raised During Deserialization
Serializing and Deserializing Complex Types
Serialization and Inheritance
Customizing the Serialized XML
Changing the XML Document Root
Changing the Element Names
Serializing Members As Attributes
Ignoring Public Members in the Serialization Process
Changing Array and Array Element Names
Ignoring Null Objects in the Serialization Process
Changing Enumeration Identifiers
Serializing and Deserializing Objects Using DataContractSerializer
Customizing the Serialized XML
Serializing and Deserializing Objects Using SoapFormatter
Customizing SOAP Serialization
Summary
Chapter 9: XML in Web Services
What Are Web Services?
Creating and Consuming Web Services
Creating a Web Service
Setting a Description for a Web Method
Adding Web Methods for CRUD Functionality
Creating a Proxy for a Web Service
Creating a Form That Calls the Web Methods
Understanding SOAP
Using SOAP Headers
Customizing the XML Serialization
Understanding the WSDL Document
The Messages
The Type Definitions
The Port Types
The Binding
The Service
A Summary of WSDL
Summary
Chapter 10: XML in WCF and Web API
Operations Based Services vs. Resource Based Services
Understanding WCF Vocabulary
Creating and Consuming a WCF Service
Creating the Service
Hosting the Service
Consuming the Service
Testing the Host and Client
Hosting a WCF Service in IIS
Understanding the Role of XML in WCF Services
Using XmlSerializer Instead of DataContractSerializer
Understanding REST Services
Creating a REST Service Using WCF
Creating a Client That Consumes the EmployeeManager REST Service
Creating a REST Service Using Web API
Creating a Client That Consumes the EmployeeManager Web API Service
Using XmlSerializer Instead of DataContractSerializer
Summary
Chapter 11: XML in SQL Server
Using XML Extensions to the SELECT Statement
The FOR XML Clause
The AUTO Mode
The RAW Mode
Returning the Schema of the XML
The PATH Mode
The EXPLICIT Mode
Specifying the Root Element Name
Using OPENXML
Using SQLXML Features
The SQLXML Managed Classes
Executing SELECT Queries
Executing Parameterized SELECT Queries
Filling a DataSet
Updating a DataSet by Using SqlXmlAdapter
Applying XSLT Templates
Writing Template Queries
Updating Data with DiffGrams
The XML Data Type
Creating a Table with an XML Column
Inserting, Modifying, and Deleting XML Data
Methods of the XML Data Type
Using the query() Method
Using the value() Method
Using the exist() Method
XML Data Modification Language (XML DML)
XQuery Support in the XML Data Type
Summary
Chapter 12: XML in .NET Framework
Using XAML to Define the WPF User Interface
Displaying XML Data in a WPF Application
Using XML in ASP.NET
XML and ASP.NET
Server Control Markup
Creating a Web Site
Designing the Web Form
Writing Code
Running the Web Application
The XML Data Source Control
Applying Transformations
Filtering Data by Using XPath Expressions
Binding an XML Data Source to a Menu Control
Working with Site Maps
Using a SiteMapPath Control
Using a SiteMapDataSource Control
Using the XML Control
Using the .NET Framework Configuration System
Structure of the web.config File
Web.config Inheritance
Using Web.config for Common Configuration Tasks
Storing and Retrieving Application Configuration Settings
Storing and Retrieving Database Connection Strings
Using Forms Authentication
Configuring Session State
Displaying Custom Error Pages
Documenting Code with XML Comments
Creating a Class Library
Documenting the Summary and Remarks
Adding Paragraphs
Documenting Method Parameters and Return Values
Specifying Scope and Permissions
Specifying Links to Other Members
Adding Lists
Generating XML Documentation from Comments
Using Sandcastle Help File Builder to Generate Help Files
Summary
Chapter 13: Working with LINQ to XML
Overview of LINQ
Working with LINQ Queries
Grouping Data Using LINQ
Sorting Data Using LINQ
Filtering Data Using LINQ
Shaping Data Using LINQ
Classic XML Technologies vs. LINQ to XML
Working with XML Fragments
Visual Construction of XML Trees
Ease in Namespace Handling
Renaming XML Nodes
Static Methods to Load XML
Whitespace Handling
XML Transformation
When to Use LINQ to XML
LINQ to XML Class Hierarchy
Opening an Existing XML Document for Parsing
Navigating Through an XML Tree
Looking for Specific Elements and Attributes
Retrieving Specific Elements Using the Descendants() Method
Searching on the Basis of Attribute Values
Modifying XML Data
Loading the XML Document
Navigating Between Various Nodes
Adding New Content
Modifying Existing Content
Deleting Existing Content
Saving the Modified XML Tree to a File
Displaying Employee Details
Events of the XElement Class
Dealing with Whitespace
Dealing with Namespaces
Specifying Namespaces While Constructing Elements
Validating XML Documents
Transforming XML Trees
Changing the Shape of an XML Tree
Projecting a New Type
Summary
Appendix A:
Creating a Custom XmlReader and XmlWriter
Creating a Custom XmlReader
Inheriting from XmlReader
Creating the TableReader Class
Declaring Class-Level Variables
Initializing the Variables
Retrieving the Total Number of Attributes
Closing the Reader
Reading Attributes
Navigating Between the Attributes
Advancing the Reader
Checking Whether the Value Is Empty
Reading Values
Returning the Table or Column Name
Returning Values
Using the TableReader Class
Creating a Custom XmlWriter
Inheriting from XmlWriter
Creating the RssWriter Class
Coding Stream-Related Operations
Defining Enumerations for RSS-Specific Tags
Writing Elements
Writing Attributes
Writing Data
Writing an XML Declaration
Using the RssWriter Class
Summary
Appendix B:
Resources
W3C Web Site for XML Specifications
W3C Web Site for XML Schema Specifications
W3C Web Site for XPath-Related Information
W3C Web Site for XSL-Related Information
W3C Web Site for SOAP Specifications
System.Xml Reference
.NET/ASP.NET/WCF/Web API
Wikipedia—XML Section
Author’s Web Site on .NET and Web Development
XML Notepad—XML Editor
Sandcastle Help File Builder
SQLXML Programming
Index