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 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
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,