Skip to Content
C# Cookbook
book

C# Cookbook

by Stephen Teilhet, Jay Hilyard
January 2004
Beginner to intermediate
864 pages
22h 18m
English
O'Reilly Media, Inc.
Content preview from C# Cookbook

Chapter 17. XML

XML (Extensible Markup Language) is a simple, portable, and flexible way to represent data in a structured format. XML is used in a myriad of ways, from acting as the foundation of web-based messaging protocols like SOAP, to being one of the more popular ways to store configuration data (such as the web.config, machine.config, or security.config files in the .NET Framework). Microsoft recognized the usefulness of XML to developers and has done a nice job of giving the developer choices around the tradeoffs one encounters when using XML. Sometimes you want to simply run though an XML document looking for a value in a read-only cursor-like fashion, and other times you need to be able to randomly access various pieces of the document. Microsoft provides classes like XmlTextReader and XmlTextWriter for lighter access and XmlDocument for full DOM (Document Object Model) processing support. It is likely that if you use .NET you will be dealing with XML to one degree or another, and in this chapter we explore some of the uses for XML and XML-based technologies like XPath and XSLT, as well as explore topics like validation of XML and transformation of XML to HTML.

17.1. Reading and Accessing XML Datain Document Order

Problem

You need to read in all the elements of an XML document and obtain information about each element, such as its name and attributes.

Solution

Create an XMLTextReader and use its Read method to process the document:

using System; using System.Xml; // ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# Cookbook

C# Cookbook

Joe Mayo
C# Cookbook, 2nd Edition

C# Cookbook, 2nd Edition

Jay Hilyard, Stephen Teilhet
ASP.NET Cookbook

ASP.NET Cookbook

Michael A Kittel, Geoffrey T. LeBlond

Publisher Resources

ISBN: 0596003390Supplemental ContentCatalog PageErrata