Blog Home  Home Feed your aggregator (RSS 2.0)  
What did you learn today? - 70-551, 70-552, 70-553 Section I, Part V - XML Serialization
Phil Denoncourt's Technology Rants
 
 Wednesday, February 01, 2006

UPGRADE: MCAD Skills to MCPD Web Developer by Using the Microsoft .NET Framework
UPGRADE: MCAD Skills to MCPD Windows Developer by Using the Microsoft .NET Framework
UPGRADE: MCSD Microsoft .NET Skills to MCPD Enterprise Application Developer by Using the Microsoft .NET Framework: Part 1

Control the serialization of an object into XML format by using the System.Xml.Serialization namespace.

  • Serialize and deserialize objects into XML format by using the XmlSerializer class.
  • Control serialization by using serialization attributes.
  • Implement XML Serialization interfaces to provide custom formatting for XML serialization.
  • Delegates and event handlers provided by the System.Xml.Serialization namespace

So the basics of using the XmlSerializer hasn't changed.  Create an XmlSerializer telling it to expect a particular type.  Note that the type could be a Generic - typeof(SortedList<int>) .  Know that you can supply override to the XML Attributes.  See my post here.  Know how to control namespaces using XmlSerializerNamespaces class.

Remember the performance problems with XML Serializer?  Creating a new dynamic assembly each time the app was restarted for XML Serialization?  There is a new tool, SGen.exe that creates the temporary serialization assemblies for you. 

Here are the XML Attributes used to control serialization:

XmlRoot – Controls the XML root.  ElementName, namespace..

XmlElementAttribute – Serialize the field/property as an element in the XML document.

XmlAttributeAttribute – Aside from being the victim of strict naming conventions, it tells the XML serializer to serialize field/prop as an attribute in the XML document

XmlIgnoreAttribute – Tells the XML Serializer to omit the field/property.

XmlEnumAttribute – Controls the name of an enumeration member (Not the enum name, a member of the enum)

 

XmlTextAttribute - Tells the XML Serializer that the member contains raw XML text.

XmlTypeAttribute - Controls the XML Schema (XSD) that is generated by the XmlSerializer.  Used to specify other namespaces and types when serializing.

 

XmlIncludeAttribute - Allows the XmlSerializer to recognize a type when it serializes or deserializes an object.  Used when deserializing custom types.

XmlChoiceIdentifierAttribute - Tells the XML Serializer that the type should be XSI:Choice.  Used in conjuction with an enum field/prop in the class to tell the serializer where to get its info from.

 

XmlArrayAttribute Class  - Specifies that the XmlSerializer must serialize a particular class member as an array of XML elements.

XmlArrayItemAttribute – Specifies the types that are contained in an XmlArray.  This is used when you are serializing polymorphic classes .

 

XmlAnyAttributeAttribute – Any attributes that are not matched up during deserialization is placed in the field decorated with this attribute.  Field must be an array of XmlAttribute.

         XmlAnyElementAttribute – Any elements that are not matched up during deserialization is placed in the field decorated with this attribute.  Field must be an array of XmlElement.

 

 

XmlNamespaceDeclarationsAttribute – Decorates a field that returns XmlSerializerNamespaces.  That field will be use to get namespace prefixes during serialization.

XmlSchemaProviderAttribute – "When applied to a type, stores the name of a static method of the type that returns an XML schema and a XmlQualifiedName that controls the serialization of the type. "  Used by WSDL.exe to return the schema for the class.  Target class must implement IXmlSerializable.  New to 2.0.

 

XmlSerializerAssemblyAttribute – Specifies the name of an assembly that the Xml Serializer can use.  If specified, the Xml Serializer doesn’t need to create a temporary assembly.  New to 2.0

XmlSerializerVersionAttribute – Signifies that the code was generated by the serialization infrastructure and can be reused for increased performance, when this attribute is applied to an assembly.  I’m not sure I should use this attribute.  I think this is used by code produced by sgen.

 

To completely control the XML Serialization of an object, you need to implement IXmlSerializable.  This is not a new interface.  In 1.1, the instructions where as follows:  "not intended to be used directly from your code.  Basically, you implement the ReadXML and WriteXML methods and you're done.

 

Here are the delegates and events:

UnknownAttribute event - thrown when the serializer encounters an unknown attribute.  By default the Xml serializer ignores unknown attributes. 

UnknownElement event - thrown when the serializer encounters an unknown element.  By default the Xml serializer ignores unknown elements. 

UnknownNode event -thrown when the serializer encounters an unknown node.  By default the Xml serializer ignores unknown node. 

UnreferencedObject event - section 5 of the SOAP document at w3c.  Basically you can reference other object within the same Xml document.  This event is thrown when it can't find the referenced object.

XmlSerializationCollectionFixupCallback delegate

XmlSerializationFixupCallback delegate

XmlSerializationReadCallback delegate

XmlSerializationWriteCallback delegate - All four of these delegates: "This delegate supports the .NET Framework infrastructure and is not intended to be used directly from your code."

 

Next post -> System.IO namespace

 

Wednesday, February 01, 2006 3:42:23 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]   Certifications  | 
Comments are closed.
Copyright © 2010 Phil Denoncourt III. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: