Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
The .NET framework offers more ways to process XML than just
parsing it. The System.Xml.Serialization namespace contains classes to create
an XML representation for an object or initialize an object directly from
XML. The focus of this of this chapter
was on developing classes with an XML representation conforming to the XSD
standard, which is widely used to describe XML formats in data exchange
scenarios.
Using XML serialization will reduce the amount of code you have
to develop for an XML-based data exchange application. You no longer have to
parse XML to initialize objects, neither do you have to develop code for
objects to persist themselves to XML. Once you defined what the XML format you
use to exchange data looks like, you can quickly develop classes that can
automatically store their data to the XML format or objects can be
automatically created from XML.
Besides the runtime support for serialization, the .NET Framework
also supplies a tool to generate source code for serializable classes from XSD
schemas. Once you start developing solutions with XSD schemas and XML
serialization make sure you read the chapter read about the XSD schema
definition tool chapter in the appendix.
Previously we introduced XML serialization. We learned how
classes are mapped to XML when we transform them with the XmlSerializer. We can
use the techniques we learned to quickly develop an XML-driven application and
get the best of both worlds: Objects for programming and XML for data transfer
or storage. The XmlSerializer does the transformations from one representation
to the other.
This will cover some advanced techniques for customizing
output of the XmlSerializer. We focused on developing classes to match an XML
format from scratch. Unfortunately there are some scenarios where this is not
good enough. Real world projects usually include classes where we can not just
go and attach an attribute when the class does not exactly map to the XML
format we need, so we will learn how to customize XML serialization at
run-time. We will also learn to manage namespaces in the serialized XML and how
to inject and retrieve XML that does not map to any class members.
You need to be familiar with the basics: How to attach metadata attributes to
classes in order to customize their XML representation as well as some other
concepts explained in the previous chapter. You also need to be familiar with:
Events and Delegates are a programming concept heavily used
throughout the .NET programming model. They provide an object-oriented and
type-safe model to register call-back methods. When a class exposes an event of
a delegate type other classes can bind event handlers to the event to receive
event notifications. If you’re not already familiar with the use of .NET events
you can find a deeper discussion on event and delegates in the .NET Framework
SDK documentation.
Often applications need to add new information
to existing XML documents or combine existing XML documents. To avoid naming
conflicts in these scenarios (“from which document is the <description>
element?”), the W3 consortium standardized XML namespaces. You can think of a
namespace as a last name for elements and attributes. Calling for somebody in a
crowd just by their first name might cause many people to respond, if you call
for somebody by their first and last name you can address the right person. XML
namespaces work the same way: the first name is an XML element or attribute;
the last name is the namespace URI. Using both, you can uniquely identify
attributes and elements in an XML document.