A

Get Paid To Write Online

Get Paid to Write at Home

Get Instant Access

The common language runtime (CLR) supports reference types called delegates. In the .NET Framework, delegates are used for event handlers and callback functions. Delegates help you assign a method to be executed or processed in response to an event that occurs in an application. Delegates act as pointers from the event to the method, which is executing code. A delegate type can represent any method with a compatible signature.

XML serialization delegates represent methods that can handle different events of an XmlSerializer class, such as the event raised when an unknown node, element, or attribute is found in the serialized stream during deserialization. You add an instance of the delegate to the event to associate the event with the event handler. The event handler is called whenever the corresponding event occurs during the process of serialization or deserialization being performed by the XmlSerializer class.

The following table describes the various XML serialization delegates that help you handle events during different situations.

Delegate

Description

UnreferencedObjectEventHandler

The UnreferencedObjectEventHandler delegate represents the method that handles the UnreferencedObject event of XmlSerializer. The UnreferencedObject event is the name of an event for which the .NET Framework provides this delegate.

XmlAttributeEventHandler

The XmlAttributeEventHandler delegate represents the method that handles UnknownAttribute of an XmlSerializer. The UnknownAttribute event occurs only when an object is being deserialized by using the Deserialize method. To associate the UnknownAttribute event with its event handler, you add an instance of the XmlAttributeEventHandler delegate to the event.

XmlElementEventHandler

The XmlElementEventHandler delegate represents the method that handles the UnknownElement event of an XmlSerializer. The UnknownElement event occurs only when you call the Deserialize method.

XmlNodeEventHandler

The XmlNodeEventHandler delegate represents the method that handles the UnknownNode event of an XmlSerializer. The UnknownNode event occurs only when you call the Deserialize method.

XmlSerializationCollectionFixupCallback

The XmlSerializationCollectionFixupCallback delegate is used by the XmlSerializer class for the deserialization of SOAP-encoded XML data types that map to collections or enumerations.

XmlSerializationFixupCallback

The XmlSerializer class uses the XmlSerializationFixupCallback delegate for the deserialization of SOAP-encoded XML data.

XmlSerializationReadCallback

The XmlSerializer class uses the XmlSerializationReadCallback delegate for the deserialization of types from SOAP-encoded and non-root XML data.

XmlSerializationWriteCallback

The XmlSerializer class uses the XmlSerializationWriteCallback delegate for the serialization of types from SOAP-encoded and non-root XML data.

The following code sample shows the use of XmlAttributeEventHandler, XmlNodeEventHandler, and XmlElementEventHandler delegates.

The code defines two classes, Company1 and Company2. Company2 contains more members than Company1. The code initially serializes an object of Company2 and then deserializes it to an object of Company1. To deal with the unknown members found during the process of deserialization, the code defines appropriate event handler methods to handle the UnknownAttribute, UnknownElement, and UnknownNode events with the help of the appropriate delegates for these events.

Visual Basic

Imports System Imports System.IO Imports System.Xml.Serialization Imports System.Xml

Public Class Company1

Public CompanyName As String End Class

<XmlRoot(ElementName:="Company1")>_ Public Class Company2

Public CompanyName As String <XmlAttribute()>_ Public CompanyType As String <XmlAttribute()>_ Public CompanyId As String Public CompanySize As String Public CompanyLocaton As String End Class

Public Class MainClass

Shared Sub Main()

Dim mainObject As MainClass

= New MainClass()

Dim filename As String = "c:

: \Delegates.xml"

Dim comp2 As Company2 = New Company2() comp2.CompanyId = "90067" comp2.CompanyLocaton = "New York" comp2.CompanyName = "ABC InfoSystems" comp2.CompanySize = "Large" comp2.CompanyType = "Technical"

Dim serializer As XmlSerializer = _

New XmlSerializer(GetType(Company2))

Dim writer As TextWriter = New StreamWriter(filename)

serializer. Serialize(writer, comp2)

Dim deserializer As XmlSerializer = _ New Xml Serializer(GetType(Company1))

writer.Close()

AddHandler deserializer.UnknownAttribute, _ AddressOf mainObject.Seriali zer_UnknownAttri bute

AddHandler deserializer.UnknownElement, _ AddressOf mainObject.Seriali zer_UnknownElement

AddHandler deserializer.UnknownNode, _ AddressOf mainObject.Seriali zer_UnknownNode

Dim fs As FileStream = New FileStream(filename, FileMode.Open) Dim comp1 As Company1 = _

Dim comp1 As Company1 = CType(e.ObjectBeingDeserialized, Company1) Console.Wri teLine(comp1.CompanyName) Console.WriteLine(sender.ToString()) End Sub

using System;

using System.IO;

using System.Xml.Serialization;

using System.Xml;

public class Company1 {

public string CompanyName;

[XmlRoot(ElementName="Company1")]

public class Company2 {

public string CompanyName;

[XmlAttribute]

public string CompanyType;

[XmlAttribute]

public string CompanyId;

public string CompanySize;

public string CompanyLocaton;

public class MainClass {

static void Main() {

MainClass mainObject = new MainClass(); string filename = "c:\\Delegates.xml"; Company2 comp2 = new Company2(); comp2.CompanyId = "90067"; comp2.CompanyLocaton = "New York"; comp2.CompanyName = "ABC InfoSystems"; comp2.CompanySize = "Large"; comp2.CompanyType = "Technical";

XmlSerializer serializer = new XmlSerializer(typeof(Company2)); TextWriter writer =new StreamWriter(filename);

serializer. Serialize(writer, comp2) ;

XmlSerializer deserializer = new XmlSerializer(typeof(Company1)); writer.Close() ;

deserializer.UnknownAttribute += new

XmlAttri buteEventHandler(mai nObject.Seri alizer_UnknownAttri bute); deserializer.UnknownElement += new

XmlElementEventHandler(mai nObject.Seri alizer_UnknownElement); deserializer.UnknownNode += new

XmlNodeEventHandler(mai nObject.Seri ali ze r_UnknownNode); FileStream fs = new FileStream(filename, FileMode.Open);

Companyl compl = (Companyl)deserializer.Deserialize(fs); fs.C1ose();

Conso1e.WriteLine("Press enter to exit"); Conso1e.ReadLine();

private void Seria1izer_UnknownNode(object sender, XmlNodeEventArgs e) {

Conso1e.WriteLine("UnknownNode Name: " + e.Name); Conso1e.WriteLine("UnknownNode LocalName: " + e.LocalName); Conso1e.WriteLine("UnknownNode Namespace URI: " + e.NamespaceURI); Conso1e.WriteLine("UnknownNode Text: " + e.Text);

Companyl compl = (Companyl)e.ObjectBeingDeserialized; Conso1e.WriteLine("Object being deserialized " + compl.ToString());

XmlNodeType myNodeType = e.NodeType; Console.WriteLine(myNodeType);

private void Seria1izer_UnknownE1ement(object sender, XmlElementEventArgs e) {

Conso1e.WriteLine("Unknown Element");

Conso1e.WriteLine(" " + e.Element.Name + " " + e.Element.InnerXml); Conso1e.WriteLine(" LineNumber: " + e.LineNumber); Conso1e.WriteLine(" LinePosition: " + e.LinePosition);

Companyl compl = (Companyl)e.ObjectBeingDeserialized; Console.Wri teLine(compl.CompanyName); Conso1e.WriteLine(sender.ToString());

private void Seria1izer_UnknownAttribute(object sender, XmlAttributeEventArgs e)

Conso1e.WriteLine("Unknown Attribute") ;

Conso1e.WriteLine(" " + e.Attr.Name + " " + e.Attr.InnerXml); Conso1e.WriteLine(" LineNumber: " + e.LineNumber); Conso1e.WriteLine(" LinePosition: " + e.LinePosition);

Companyl compl = (Companyl)e.ObjectBeingDeserialized; Console.Wri teLine(compl.CompanyName); Conso1e.WriteLine(sender.ToString());

Discussion: Identifying When to Use a Specific Type of Serialization

In this discussion, you will discuss when to use a specific type of XML serialization in a specific situation.

Was this article helpful?

0 0

Post a comment