To whoever can help,
I've been having a problem with XML deserialization lately.
I needed to serialize and deserialze two objects which inherited from
Hashtable. Because IDictionary implementations cannot be serialized, I had
to take matters into my own hands by implementing a wrapper over the
Hashtable which implemted IXmlSerializabl e.
I called it XmlHashtable. It has, among other convenience methods, a method
called ReadXml(XmlRead er) and a method called WriteXml(XmlWri ter). This is
an abstract class which also contains two abstract properties which return
strings so I could have inheriting classes choose the names they wish for the
root element name and the name for individual items.
I implemented my own, because though I've seen code on the web which is
similar, I needed to be able to use actual objects as values, and not just
strings. This code works with any Type you wish to put into the Hashtable
that is available at runtime. I've tested it with both native Types and some
of my user defined types.
The code looks like this:
<code>
[XmlType( "XmlDict" )]
public abstract class XmlHashtable : Hashtable, IXmlSerializabl e {
/// <summary>
/// Returns the local name of the root node for the class. Implementing
/// classes must override this property.
/// </summary>
protected abstract string LocalName {get;}
/// <summary>
/// Returns the local name of the name of items in the dictionary.
/// Implementing classes must override this property.
/// </summary>
protected abstract string ItemType { get;}
/// <summary>
/// Generates an <see cref="XmlHashta ble"/> from its XML representation.
/// </summary>
/// <param name="reader">T he <see cref="XmlReader "/> from which the
/// object is deserialized</param>
public void ReadXml ( System.Xml.XmlR eader reader ) {
try {
string key = null;
object value = null;
Type type = null;
reader.ReadToFo llowing ( LocalName );
if ( reader.IsStartE lement ( LocalName ) ) {
reader.ReadStar tElement ( LocalName );
while ( reader.NodeType != XmlNodeType.End Element ) {
key = reader.GetAttri bute ( "Key" );
type = Type.GetType ( reader.GetAttri bute ( "Type" ) );
reader.ReadStar tElement ( ItemType );
reader.ReadStar tElement ( "Value" );
XmlSerializer serializer = new XmlSerializer ( type );
value = serializer.Dese rialize ( reader.ReadSubt ree( ) );
this.Add ( key, value );
reader.ReadEndE lement ( ); // Reading end node for Name
reader.ReadEndE lement ( ); // Reading end node for Value
reader.ReadEndE lement ( ); // Reading end node for
Element
reader.MoveToCo ntent ( );
}
reader.ReadEndE lement ( ); // Reading end root node
}
}
catch ( XmlException e ) {
throw new XmlException ( "XmlDictionary. ReadXml(XmlRead er)
reader pointing to invalid element", e );
}
}
/// <summary>
/// Converts an <see cref="XmlHashta ble"/> object into its XML
/// representation.
/// </summary>
/// <param name="writer">T he <see cref="XmlWriter "/> stream to which the
/// object is serialized</param>
public void WriteXml ( System.Xml.XmlW riter writer ) {
writer.WriteSta rtElement ( LocalName );
foreach ( DictionaryEntry de in this ) {
writer.WriteSta rtElement ( ItemType );
writer.WriteAtt ributeString ( "Key", de.Key.ToString ( ) );
writer.WriteAtt ributeString ( "Type", de.Value.GetTyp e (
).FullName );
writer.WriteSta rtElement ( "Value" );
XmlSerializer serializer = new XmlSerializer ( de.Value.GetTyp e
( ) );
serializer.Seri alize ( writer, de.Value );
writer.WriteEnd Element ( );
writer.WriteEnd Element ( );
}
writer.WriteEnd Element ( );
}
}
</code>
The inheriting classes must use the [XmlSchemaProvid er( "GetXmlSche ma" )]
attribute, which they do as shown here:
<code>
/// <summary>
/// Adds the XML schema to the <see cref="XmlSchema Set"/> and returns
/// the fully qualified name for the <see cref="AssetProp erties"/>
/// root element.
/// </summary>
/// <param name="set">The <see cref="XmlSchema Set"/> to add the schema
/// to</param>
/// <returns><see cref="XmlQualif iedName"/> representing the
/// schema for this type</returns>
public static XmlQualifiedNam e GetXmlSchema ( XmlSchemaSet set ) {
XmlSchemaAttrib ute attribute;
XmlSchemaSequen ce sequence;
XmlSchemaElemen t element;
XmlSchemaComple xType itemType = new XmlSchemaComple xType ( );
itemType.Name = ItemType + "Type";
// create the "Key" attribute and add it to the complex item type
attribute = new XmlSchemaAttrib ute ( );
attribute.Name = "Key";
attribute.Schem aTypeName = new XmlQualifiedNam e ( "string",
"http://www.w3.org/2001/XMLSchema" );
attribute.Use = XmlSchemaUse.Re quired;
itemType.Attrib utes.Add ( attribute );
// create the "Type" attribute and add it to the complex item type
attribute = new XmlSchemaAttrib ute ( );
attribute.Name = "Type";
attribute.Schem aTypeName = new XmlQualifiedNam e ( "string",
"http://www.w3.org/2001/XMLSchema" );
attribute.Use = XmlSchemaUse.Re quired;
itemType.Attrib utes.Add ( attribute );
sequence = new XmlSchemaSequen ce ( );
element = new XmlSchemaElemen t ( );
element.Name = "Value";
element.SchemaT ypeName = new XmlQualifiedNam e ( "anyType",
"http://www.w3.org/2001/XMLSchema" );
sequence.Items. Add ( element );
itemType.Partic le = sequence;
XmlSchemaComple xType assetProperties Type = new
XmlSchemaComple xType ( );
assetProperties Type.Name = LocalName + "Type";
// create "AssetPropertie sType" schema type for
"AssetPropertie s" element
sequence = new XmlSchemaSequen ce ( );
element = new XmlSchemaElemen t ( );
element.Name = XmlCoreTypes.As setProperties.I temType;
element.MaxOccu rsString = "unbounded" ;
element.MinOccu rsString = "0";
element.SchemaT ypeName = new XmlQualifiedNam e ( itemType.Name,
"http://mediaframe.com" );
sequence.Items. Add ( element );
assetProperties Type.Particle = sequence;
element = new XmlSchemaElemen t ( );
element.Name = LocalName;
element.SchemaT ypeName = new XmlQualifiedNam e (
assetProperties Type.Name, "http://mediaframe.com" );
// Generate the Schema element itself and set the necessary
properties
XmlSchema schema = new XmlSchema ( );
schema.TargetNa mespace = "http://mediaframe.com" ;
schema.Namespac es.Add ( "mf", "http://mediaframe.com" );
schema.Items.Ad d ( element );
schema.Items.Ad d ( itemType );
schema.Items.Ad d ( assetProperties Type );
// Add the schema to the set of schemas and compile it
set.XmlResolver = new XmlUrlResolver ( );
set.Add ( schema );
set.Compile ( );
//return element.Qualifi edName;
return assetProperties Type.QualifiedN ame;
}
</code>
So, serializing and deserializing one of these objects by itself works
perfectly. However, when I try to serialize an array of these objects and
then deserialize it, I only get the first one deserialized before it all
quits on me. The serialization works fine, I can see the XML by putting a
break point before deserializing.
I'm trying to use this in my web services, but it has the same issue whether
I serialize and deserialize manually (with the XmlSerializer) or use the web
services to do it all.
I'm just trying to figure out where I went wrong. Any tips would be
extremely useful.
Thanks,
John Glover