BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
XmlSerializer
Event notifications
Serializing Objects
any and anyAttribute
XmlAnyElement
Serializing XML nodes
Choice Model Groups
Generic XmlSerializer
Runtime Overrides
Runtime Customization
Customizing Xml Serialization
Advanced XmlSerializer
XmlSerializer Attributes
Runtime exceptions
Serializing Collection Classes
Serializing Arrays
XmlElement Attribute Type
XmlIncludeAttribute
XmlSerializer
Serialization Attributes
Serializable Classes
<< XML DOM
XQuery >>

By :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.
First posted :03/24/2008
Times viewed :1957

 

Serialization Namespaces

All the way through the last one and a half chapters we have barely mentioned XML namespaces, but that does not imply that the XmlSerializer does not support them. In fact, the XmlSerializer recognizes the important role XML namespaces are playing when systems exchange data through XML and supports them through a variety of features:

  • Default Namespaces per serializer instance
  • Static XML Namespace declaration per class or field in the class’ source code
  • Static namespace prefix declaration in the class’ source code
  • Dynamic XML Namespace declaration per class or field at runtime
  • Dynamic namespace prefix declaration at runtime

This section will take a closer look at each one of them, enabling us develop XML namespace enabled solutions with the XmlSerializer.

Declaring namespaces in the source code

Two of the namespace support features of the XmlSerializer enable namespace management inside the source code of a class. When we develop classes to bind data from  XML schema types we can add the namespaces directly to the source code by attaching metadata attributes. The XmlSerializer reads the namespace information and produces the namespace declarations upon serialization of an object. Of course, it will also account for namespace correct-ness when it is deserializing an XML stream. If a metadata attributes defines for a field to bind to an XML node from a specific namespace then the XmlSerializer will only populate that field from an XML node where node name and namespace match the attribute.

The seconds feature allows providing prefix declarations for namespaces, but more on that later, first we will see how to define namespace relationships in the source code.

Declaring namespaces for individual attributes and elements

You may have noticed the Namespace property on most of the metadata attributes when you studied table 9.4, but we have yet to examine what they do. When we attach XML serialization attribute to a field, we can set the attribute’s Namespace property to tell the XmlSerializer which XML namespace the element or attribute corresponding to the field belongs to. We can declare XML namespaces at different levels inside a class.

At the highest level, we can specify the XML namespace of the schema type with class’ XmlRoot attribute. However, the XmlSerializer does not consider XmlRoot when an object is not at the top of a serialization hierarchy. This is not a problem if all classes in an object model correspond to types from the same XML schema. However, if we model a schema that imports types from different namespaces, our classes have to reflect this at the field and property definitions. We declare the external namespaces by attaching metadata attributes to fields referencing an imported type.

Let’s put see how this works when we actually design a class! We start out with an XML schema to describe a type named Car_T. The schema imports types from two other namespaces. One is referenced by an attribute; one is referenced by an element of the Car_T type. The Car_T type declares another element, Year, which is part of the schema’s namespace (listing 10.7).

11        Listing 10.7 A schema importing types from two other namespaces

<?xml version="1.0" encoding="utf-8"?>

<xs:schema xmlns:tns="urn:christoph-types-cars"

  targetNamespace="urn:christoph-types-cars"

  xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:import namespace="urn:cars-models" />

  <xs:import namespace="urn:cars-makes" />

  <xs:element name="Car" type="tns:Car_T" />

  <xs:complexType name="Car_T">

    <xs:sequence>

      <xs:element minOccurs="0" maxOccurs="1"

        xmlns:q1="urn:cars-models" ref="q1:Model" />

      <xs:element minOccurs="1" maxOccurs="1" name="Year" />

    </xs:sequence>

    <xs:attribute xmlns:q3="urn:cars-makes" ref="q3:Make" />

  </xs:complexType>

</xs:schema>

Then we write a Car class that the XmlSerializer will relate to the Car_T type. To make sure the XmlSerializer includes the correct namespace declaration when we serialize a Car object we specify the namespace through an XmlRoot attribute attached to the class. We also have to attach XmlAttribute and XmlElement attributes on the fields corresponding to the types imported from another namespace. Note that we have to declare namespaces on fields even when their classes already contain XmlRoot attributes to declare their XML namespace.

12        Listing 10.8 A class with XML namespace declarations

[XmlRoot("Car", Namespace="urn:christoph-types" )]

public class Car

{

  public Car() {}

  [XmlAttribute(Namespace="urn:cars-makes")]

  public string Make;

  [XmlElement(Namespace="urn:cars-models")]

  public string Model;

  public int Year;

}

When the XmlSerializer serializes an instance of this class, it automatically includes the namespace declarations from attached attributes. The root element declares the XML namespace we specified to the XmlRoot attribute as the type’s default namespace. It also generates the declaration of the Make attribute with a dummy prefix, since the attribute does not belong to the default namespace. Finally, it declares the Model element’s namespace through another local namespace declaration on the model element.

13        Listing 10.9 A serialized instance of the Car class

<Car

  d3p1:Make="Ford"

  xmlns:d3p1="urn:cars-makes"

  xmlns="urn:christoph-types-cars" …>

      <Model xmlns="urn:cars-models">Explorer</Model>

     <Year>1997</Year>

</Car>


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

Integration Specialist Needed - Wor
Virtualization Server Infrastructur
A great opportunity to Digital Vide
here is a greate opportunity as a S
A great opportunity as a Network En

View all Jobs (Add yours)
View all CV (Add yours)




swimming pool contractor
chicago web site design
United Kingdom Conference Calling
Web Hosting
online fax
designer sunglasses
answering service


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP