BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
XmlSerializer
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
Deserializing an object
<< 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 :915

 

Event notifications

The second way to gain access to unmapped XML nodes in an XML stream that is deserialized is to register event handlers with the XmlSerializer. Attaching the XmlAnyAttribute or XmlAnyElement attributes works great where we anticipate the content we process to contain unmapped nodes on a regular basis. Registering event handlers for unmapped nodes on the other hand works better for cases where we want an application to handle them as an exception rather than as the rule. The XmlSerializer raises four different events for unmapped nodes when it is deserializing a class without XmlAnyAttribute or XmlAnyElement attributes. We can register event sinks for the four events listed in table 10.2 to receive notifications for unexpected XML in the stream.

1.5            Table 10.2 Events defined by the XmlSerializer

Event

Description

void UnknownAttribute(Object sender,
XmlAttributeEventArgs args)

Fires when the XmlSerializer encounters an XML attribute without a corresponding class field during deserialization.

void UnknownElement(Object sender,
XmlElementEventArgs args)

Fires when the XmlSerializer encounters an XML element without a corresponding class field during deserialization.

void UnknownNode(Object sender,
XmlNodeEventArgs args)

Fire when the XmlSerializer encounters an XML node of unknown type during deserialization.

void UnreferencedObject(Object sender,
UnreferencedObjectEventArgs args)

Fires during deserialization of a SOAP-encoded XML stream.

à ?? KELLY ?? ß

The XmlSerializer fires one of these events for each unmapped node it encounters while deserializing an object, but object’s class does not designate fields with the XmlAnyAttribute or the XmlAnyElement. Each event provides details about the unmapped node in the form of a event specific arguments class passed to the event handler. For example, when the XmlSerializer fires and for an unmapped attribute in the XML stream, it passes a reference to itself and an XmlAttributeEventArgs object to the registered event handler. The arguments object contains the line number and position of the attribute within the deserialized XML document, as well as the attribute itself.

The following example shows how to set up event handlers to log the event details about nodes the XmlSerializer could not map to any class members to the console.

10        Listing 10.5 Logging nodes the XmlSerializer can not map to class fields during deserialization

public void BindSerializerEvents( XmlSerializer ser )
{

  ser.UnknownAttribute +=                              | #1

    new XmlAttributeEventHandler(OnUnknownAttribute);  | #2

  ser.UnknownElement +=

    new XmlElementEventHandler(OnUnknownElement);

  ser.UnknownNode +=

    new XmlNodeEventHandler(OnUnknownNode);

}

public static void OnUnknownAttribute(Object sender,

      XmlAttributeEventArgs args)

{

  string typeName = "N/A";

  if( args.ObjectBeingDeserialized != null )

  {

    typeName = 
      args.ObjectBeingDeserialized.GetType().FullName;

  }

  Console.WriteLine(                                   |#3

    @"Unknown attribute {0}:{1}='{2}' at line {3},      |
    position {4}, type: {5})",                         |
    args.Attr.NamespaceURI,   args.Attr.LocalName,     |
    args.Attr.Value, args.LineNumber,                  |

    args.LinePosition, typeName );

}

public static void OnUnknownElement(Object sender,    XmlElementEventArgs args)

{

  string typeName = "N/A";

  if( args.ObjectBeingDeserialized != null )

  {

    typeName = 
      args.ObjectBeingDeserialized.GetType().FullName;

  }

  Console.WriteLine(                                  |#4

    "Unknown element {0}:{1}='{2}' at line {3},       |
    position {4}, type: {5})",                        |

    args.Element.NamespaceURI,                        |
    args.Element.LocalName, args.Element.InnerXml,    |
    args.LineNumber, args.LinePosition, typeName);    |

}

public static void OnUnknownNode(Object sender,
      XmlNodeEventArgs args)

{

  string typeName = "N/A";

  if( args.ObjectBeingDeserialized != null )

  {

    typeName =
      args.ObjectBeingDeserialized.GetType().FullName;

  }

  Console.WriteLine(                                   |#5

    "Unkown Node type {6} {0}:{1}='{2}' at line {3},   |
    position {4}, type: {5})",                         |
    args.NamespaceURI, args.LocalName, args.Text,      |
    args.LineNumber, args.LinePosition, typeName,      |

    args.NodeType.ToString() );                        |

}

(annotation) <#1 The += operator is used to register an event sink >

(annotation) <#2 We have to instantiate a delegate object for the event sink method>

(annotation) <#3 The XmlAttributeEventArgs object contains detailed information about the unmapped attribute.>

(annotation) <#4 The XmlElementEventArgs object contains detailed information about the unmapped element.>

(annotation) <#5 The XmlNodeEventArgs object contains detailed information about the unmapped node.>


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
fax online
Domain Names
online fax
D&g 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