Contents....5
About the Author....16
About the Technical Reviewer....17
Introduction....18
Chapter 1: Introducing XML and the .NET Framework....20
What Is XML?....20
Benefits of XML....21
XML Is an Industry Standard....21
XML Is Self-Describing....21
XML Is Extensible....21
XML Can Be Processed Easily....21
XML Can Be Used to Easily Exchange Data....22
XML Can Be Used to Easily Share Data....22
XML Can Be Used to Create Specialized Vocabularies....22
XML-Driven Applications....22
Rules of XML Grammar....24
Markup Is Case Sensitive....25
A Document Must Have One and Only One Root Element....25
A Start Tag Must Have an End Tag....25
Start and End Tags Must Be Properly Nested....26
Attribute Values Must Be Enclosed in Quotes....26
DTD and XML Schema....26
Parsing XML Documents....27
XSLT....28
XPath....29
The .NET Framework....30
.NET Framework and XML....31
Assemblies and Namespaces....32
System.Xml Namespace....32
System.Xml.Schema Namespace....32
System.Xml.XPath Namespace....32
System.Xml.Xsl Namespace....32
System.Xml.Serialization Namespace....32
System.Xml.Linq Namespace....33
The Classic XML Parsing Model of the .NET Framework....33
The LINQ-Based Parsing Model of the .NET Framework....33
.NET Configuration Files....34
ADO.NET....36
ASP.NET Web Forms Server Controls....36
XML Serialization....37
Web Services, WCF Services, and Web API....38
XML Documentation....38
XAML Markup....40
SQL Server XML Features....40
Working with Visual Studio....40
Creating Windows Forms Applications....41
Creating Class Libraries....44
Summary....47
Chapter 2: Manipulating XML Documents Using the Document Object Model....48
Using the DOM Parser....48
Knowing When to Use DOM....50
A Sample XML Document....51
Opening an Existing XML Document for Parsing....52
Navigating Through an XML Document....54
Looking for Specific Elements and Nodes....55
Retrieving Specific Elements Using the GetElementsByTagName() Method....56
Retrieving Specific Elements Using the GetElementById() Method....57
Selecting Specific Nodes Using the SelectNodes() Method....60
Selecting a Single Specific Node Using the SelectSingleNode() Method....62
Modifying XML Documents....63
Navigating Between Various Nodes....64
Modifying Existing Content....65
Deleting Existing Content....66
Adding New Content....67
Using Helper Methods....69
Dealing with Whitespace....69
Dealing with Namespaces....73
Understanding Events of the XmlDocument Class....74
Summary....77
Chapter 3: Reading and Writing XML Documents....78
What Are XML Readers and Writers?....78
When to Use Readers and Writers....79
Reader Classes....79
The XmlTextReader Class....79
The XmlValidatingReader Class....79
The XmlNodeReader Class....79
Reading XML Documents Using XmlTextReader....80
Opening XML Documents....80
Reading Attributes, Elements, and Values....82
Improving Performance by Using Name Tables....84
Dealing with Namespaces....85
Moving Between Elements....85
The ReadSubTree() Method....85
The ReadToDescendant() Method....86
The ReadToFollowing() Method....86
The ReadToNextSibling() Method....87
The Skip() Method....87
Moving Between Attributes....88
Reading Content....88
The ReadInnerXml() Method....89
The ReadOuterXml() Method....89
The ReadString() Method....89
Writing XML Documents Using XmlTextWriter....90
Exporting Columns As Elements....94
Exporting Columns As Attributes....94
Specifying Character Encoding....94
Formatting the Output....95
Including Namespace Support....97
Dealing with Nontextual Data....100
Serializing Data....101
Deserializing Data....102
Summary....103
Chapter 4: Accessing XML Documents Using the XPath Data Model....104
Overview of XPath....104
Location Path....105
Axis....105
Node Tests....105
Predicates....106
Putting It All Together....106
XPath Functions....106
The XPath Data Model....108
Creating XPathNavigator....108
Navigating an XML Document Using XPathNavigator....109
Selecting Nodes....111
Selecting Single Nodes....113
Selecting Children, Ancestors, and Descendants....114
Compiling XPath Expressions....115
Navigating Between Attributes....115
Retrieving Inner and Outer XML....116
Getting an XmlReader from XPathNavigator....118
Getting an XmlWriter from XPathNavigator....121
Editing XML Documents with the XPathNavigator Class....123
Adding Nodes....125
Modifying Nodes....126
Deleting Nodes....127
Saving Changes....128
Summary....128
Chapter 5: Validating XML Documents....129
Providing Structure for XML Documents....129
Document Type Definition (DTD)....130
XML Data Reduced (XDR) Schema....130
XML Schema Definition (XSD) Schema....130
Creating Structure for an XML Document....130
The Structure of Employees.xml....130
Creating the DTD....131
Creating the XML Schema....133
XSD for Employees.xml....134
Creating the XSD in Visual Studio Designer....134
Creating the Schema from XML Document Using Visual Studio IDE....140
Creating the Schema by Using the XML Schema Definition Tool....141
Creating the Schema from an XML Document....142
Creating the Schema from an Assembly....143
Creating Schemas by Using the Schema Object Model (SOM)....144
The Core SOM Classes....144
Creating an XML Schema Using the SOM....146
Creating the Schema....149
Creating a Simple Type for Names....149
Creating a Simple Type for Phone Numbers....149
Creating a Simple Type for Notes....150
Creating a Complex Type That Represents an Employee....150
Creating a Top-Level Complex Type....151
Creating the Root Element....152
Compiling the Schema....152
Saving the Schema....152
Attaching the DTD and XML Schemas to XML Documents....153
Inline DTDs....154
External DTDs....154
Inline XML Schema....155
External XML Schema....155
Adding Frequently Used Schemas to the Schema Cache....156
Using XmlReader to Validate XML Documents....157
Using XmlDocument to Validate XML Documents....160
Using XPathNavigator to Validate XML Documents....163
Specifying XML Schema via Code....163
Summary....165
Chapter 6: Transforming XML with XSLT....166
Overview of XSLT....166
Applying Templates Using ....170
Branching Using ....171
Branching Using and ....173
Transforming Elements and Attributes....175
The XslCompiledTransform Class....178
Performing Transformations Using XslCompiledTransform....178
Passing Arguments to a Transformation....181
Using Script Blocks in an XSLT Style Sheet....183
Using Extension Objects....187
Compiling XSLT Style Sheets....188
Summary....190
Chapter 7: XML in ADO.NET....191
Overview of ADO.NET Architecture....191
Connected Data Access....191
Disconnected Data Access....192
ADO.NET Data Providers....193
SQL Server Data Provider....193
OLEDB Data Provider....193
ODBC Data Provider....193
Oracle Data Provider....194
The Assemblies and Namespaces Involved....194
ADO.NET Classes....194
Connection (SqlConnection and OleDbConnection)....194
Command (SqlCommand and OleDbCommand)....195
Parameter (SqlParameter and OleDbParameter)....195
DataReader (SqlDataReader and OleDbDataReader)....195
DataAdapter (SqlDataAdapter and OleDbDataAdapter)....195
DataSet....196
XML and Connected Data Access....196
Using the ExecuteXmlReader() Method....196
XML and Disconnected Data Access....198
Understanding DataSet....199
Understanding DataAdapter....200
Working with DataSet and DataAdapter....202
Filling a DataSet....202
Accessing Data from DataSet....203
Adding New Rows....204
Updating an Existing Row....204
Deleting a Row....205
Using DataRow States....205
Saving the Changes to the Database....206
Saving DataSet Contents As XML....208
Saving Only the Schema....212
Extracting DataSet Contents As an XML String....213
Reading XML Data into DataSet....213
Using the Automatic Read Operation....215
Reading DiffGrams....215
Reading XML Fragments....215
Ignoring Schema Information....216
Inferring Schema Information....216
Reading Schema Information....216
Generating Menus Dynamically Based On an XML File....216
Reading Only the Schema Information....218
Creating a Typed DataSet....220
Using Visual Studio to Create a Typed DataSet....221
Using the xsd.exe Tool to Create a Typed DataSet....226
Summary....226
Chapter 8: XML Serialization....227
Understanding the Flavors of Serialization....227
Classes Involved in the XML Serialization....228
Serializing and Deserializing Objects Using XmlSerializer....228
Handling Events Raised During Deserialization....231
Serializing and Deserializing Complex Types....233
Serialization and Inheritance....237
Customizing the Serialized XML....240
Changing the XML Document Root....242
Changing the Element Names....242
Serializing Members As Attributes....242
Ignoring Public Members in the Serialization Process....243
Changing Array and Array Element Names....243
Ignoring Null Objects in the Serialization Process....243
Changing Enumeration Identifiers....243
Serializing and Deserializing Objects Using DataContractSerializer....243
Customizing the Serialized XML....246
Serializing and Deserializing Objects Using SoapFormatter....247
Customizing SOAP Serialization....250
Summary....253
Chapter 9: XML in Web Services....254
What Are Web Services?....254
Creating and Consuming Web Services....255
Creating a Web Service....256
Setting a Description for a Web Method....262
Adding Web Methods for CRUD Functionality....262
Creating a Proxy for a Web Service....269
Creating a Form That Calls the Web Methods....271
Understanding SOAP....274
Using SOAP Headers....275
Customizing the XML Serialization....279
Understanding the WSDL Document....280
The Messages....281
The Type Definitions....281
The Port Types....282
The Binding....282
The Service....282
A Summary of WSDL....282
Summary....282
Chapter 10: XML in WCF and Web API....283
Operations Based Services vs. Resource Based Services....284
Understanding WCF Vocabulary....284
Creating and Consuming a WCF Service....285
Creating the Service....285
Hosting the Service....290
Consuming the Service....293
Testing the Host and Client....296
Hosting a WCF Service in IIS....298
Understanding the Role of XML in WCF Services....301
Using XmlSerializer Instead of DataContractSerializer....302
Understanding REST Services....303
Creating a REST Service Using WCF....304
Creating a Client That Consumes the EmployeeManager REST Service....308
Creating a REST Service Using Web API....312
Creating a Client That Consumes the EmployeeManager Web API Service....315
Using XmlSerializer Instead of DataContractSerializer....317
Summary....318
Chapter 11: XML in SQL Server....319
Using XML Extensions to the SELECT Statement....319
The FOR XML Clause....319
The AUTO Mode....320
The RAW Mode....321
Returning the Schema of the XML....322
The PATH Mode....323
The EXPLICIT Mode....323
Specifying the Root Element Name....327
Using OPENXML....327
Using SQLXML Features....329
The SQLXML Managed Classes....329
Executing SELECT Queries....330
Executing Parameterized SELECT Queries....332
Filling a DataSet....333
Updating a DataSet by Using SqlXmlAdapter....334
Applying XSLT Templates....336
Writing Template Queries....339
Updating Data with DiffGrams....340
The XML Data Type....342
Creating a Table with an XML Column....343
Inserting, Modifying, and Deleting XML Data....343
Methods of the XML Data Type....344
Using the query() Method....345
Using the value() Method....345
Using the exist() Method....345
XML Data Modification Language (XML DML)....346
XQuery Support in the XML Data Type....347
Summary....347
Chapter 12: XML in .NET Framework....348
Using XAML to Define the WPF User Interface....348
Displaying XML Data in a WPF Application....353
Using XML in ASP.NET....355
XML and ASP.NET....356
Server Control Markup....356
Creating a Web Site....356
Designing the Web Form....357
Writing Code....361
Running the Web Application....363
The XML Data Source Control....364
Applying Transformations....367
Filtering Data by Using XPath Expressions....369
Binding an XML Data Source to a Menu Control....370
Working with Site Maps....372
Using a SiteMapPath Control....374
Using a SiteMapDataSource Control....375
Using the XML Control....376
Using the .NET Framework Configuration System....378
Structure of the web.config File....380
Web.config Inheritance....380
Using Web.config for Common Configuration Tasks....380
Storing and Retrieving Application Configuration Settings....381
Storing and Retrieving Database Connection Strings....382
Using Forms Authentication....384
Configuring Session State....387
Displaying Custom Error Pages....389
Documenting Code with XML Comments....391
Creating a Class Library....391
Documenting the Summary and Remarks....392
Adding Paragraphs....392
Documenting Method Parameters and Return Values....393
Specifying Scope and Permissions....393
Specifying Links to Other Members....394
Adding Lists....394
Generating XML Documentation from Comments....395
Using Sandcastle Help File Builder to Generate Help Files....396
Summary....398
Chapter 13: Working with LINQ to XML....399
Overview of LINQ....399
Working with LINQ Queries....400
Grouping Data Using LINQ....402
Sorting Data Using LINQ....404
Filtering Data Using LINQ....404
Shaping Data Using LINQ....405
Classic XML Technologies vs. LINQ to XML....405
Working with XML Fragments....405
Visual Construction of XML Trees....406
Ease in Namespace Handling....406
Renaming XML Nodes....406
Static Methods to Load XML....406
Whitespace Handling....406
XML Transformation....406
When to Use LINQ to XML....406
LINQ to XML Class Hierarchy....407
Opening an Existing XML Document for Parsing....407
Navigating Through an XML Tree....409
Looking for Specific Elements and Attributes....411
Retrieving Specific Elements Using the Descendants() Method....411
Searching on the Basis of Attribute Values....412
Modifying XML Data....414
Loading the XML Document....415
Navigating Between Various Nodes....415
Adding New Content....416
Modifying Existing Content....417
Deleting Existing Content....417
Saving the Modified XML Tree to a File....418
Displaying Employee Details....418
Events of the XElement Class....419
Dealing with Whitespace....420
Dealing with Namespaces....423
Specifying Namespaces While Constructing Elements....424
Validating XML Documents....425
Transforming XML Trees....427
Changing the Shape of an XML Tree....428
Projecting a New Type....430
Summary....431
Appendix A: Creating a Custom XmlReader and XmlWriter....432
Creating a Custom XmlReader....432
Inheriting from XmlReader....433
Creating the TableReader Class....434
Declaring Class-Level Variables....438
Initializing the Variables....438
Retrieving the Total Number of Attributes....439
Closing the Reader....439
Reading Attributes....439
Navigating Between the Attributes....440
Advancing the Reader....441
Checking Whether the Value Is Empty....441
Reading Values....441
Returning the Table or Column Name....442
Returning Values....442
Using the TableReader Class....442
Creating a Custom XmlWriter....444
Inheriting from XmlWriter....445
Creating the RssWriter Class....446
Coding Stream-Related Operations....447
Defining Enumerations for RSS-Specific Tags....447
Writing Elements....447
Writing Attributes....449
Writing Data....450
Writing an XML Declaration....451
Using the RssWriter Class....452
Summary....453
Appendix B: Resources....454
W3C Web Site for XML Specifications....454
W3C Web Site for XML Schema Specifications....454
W3C Web Site for XPath-Related Information....454
W3C Web Site for XSL-Related Information....454
W3C Web Site for SOAP Specifications....454
System.Xml Reference....454
.NET/ASP.NET/WCF/Web API....455
Wikipedia—XML Section....455
Author’s Web Site on .NET and Web Development....455
XML Notepad—XML Editor....455
Sandcastle Help File Builder....455
SQLXML Programming....455
Index....456
Master the basics of XML as well as the namespaces and objects you need to know in order to work efficiently with XML. You’ll learn extensive support for XML in everything from data access to configuration, from raw parsing to code documentation. You will see clear, practical examples that illustrate best practices in implementing XML APIs and services as part of your C#-based Windows 10 applications.Beginning XML with C# 7 is completely revised to cover the XML features of .NET Framework 4.7 using C# 7 programming language. In this update, you’ll discover the tight integration of XML with ADO.NET and LINQ as well as additional .NET support for today’s RESTful web services and Web API.Written by a Microsoft Most Valuable Professional and developer, this book demystifies everything to do with XML and C# 7.
Those with experience in C# and .NET new to the nuances of using XML. Some XML experience is helpful.