Happy new year to all.
I have a strange error that I've been trying for a while now to
fathom..
Basically I have a hierarchy of state classes that I need to serialize
to XML. Some of them can contain Exceptions, so I've decided to
implement IXmlSerializable to get round the fact that Exception is not
serializable.
Here's the implementation code from the (abstract) superclass:
#region IXmlSerializable Members
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteAttributeString(XmlNamespaces.XmlSchem aInstance.Prefix,
"type", XmlNamespaces.XmlSchemaInstance.Uri.ToString(),
this.GetType().Name);
WriteAdditionalContentXml(writer);
}
public System.Xml.Schema.XmlSchema GetSchema()
{
XmlSchema schema = new XmlSchema();
schema.Id = this.GetType().FullName;
schema.TargetNamespace = XmlNamespaces.Service.Uri.ToString();
schema.Namespaces.Add(XmlNamespaces.Service.Prefix ,
XmlNamespaces.Service.Uri.ToString());
schema.Namespaces.Add(XmlNamespaces.XmlSchemaInsta nce.Prefix,
XmlNamespaces.XmlSchemaInstance.Uri.ToString());
XmlSchemaComplexType state = new XmlSchemaComplexType();
state.Name = this.GetType().Name;
schema.Items.Add(state);
XmlSchemaSequence sequence = new XmlSchemaSequence();
sequence.MinOccurs = 0;
sequence.MaxOccurs = 1;
AddSchemaContent(schema, sequence);
if (sequence.Items.Count > 0)
{
state.Particle = sequence;
}
schema.Compile(new ValidationEventHandler(schemaValidationHandler));
return schema;
}
public virtual void ReadXml(System.Xml.XmlReader reader)
{
}
protected virtual void WriteAdditionalContentXml(XmlWriter writer)
{
}
protected virtual void AddSchemaContent(XmlSchema schema,
XmlSchemaSequence parentSequence)
{
}
private void schemaValidationHandler(object sender,
ValidationEventArgs args)
{
Trace.WriteLine(args.Message);
Debug.Fail(args.Message + ":" + Environment.NewLine +
Environment.NewLine + args.Exception.ToString() + Environment.NewLine +
Environment.NewLine);
}
#endregion
The problem is this. I can (de)serialize whichever type is specified as
the first argument in the XmlSerializer constructor. However, attempts
to serialize any types that are specified in the additional types
argument fail with an InvalidOperationException ("There was an error
generating the XML document"), the inner exception of which is an
InvalidCastException ("Specified cast is not valid"), and the stack
trace of which is:
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o, XmlSerializerNamespaces namespaces, String
encodingStyle)
at System.Xml.Serialization.XmlSerializer.Serialize(X mlWriter
xmlWriter, Object o)
at
Limit.Company.LossAdjustment.Tests.State.StatesTes t.serialize(FeeState
state, XmlSerializer serializer) in
q:\projects\limit\limit.company.lossadjustment.tes ts\state\statestest.cs:line
131
at
Limit.Company.LossAdjustment.Tests.State.StatesTes t.XmlSerializeErrorState()
in
q:\projects\limit\limit.company.lossadjustment.tes ts\state\statestest.cs:line
71
--InvalidOperationException
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlS erializationWriter1.Write1_State(Object
o)
Furthermore, if I explicitly create XML that matches the schema of one
of the additional types, it deserializes, but to the type that was
specified as the FIRST argument of the XmlSerializer constructor. I
guess this means that I have problems with my schemas somewhere, but
I'm at a loss as to how to fix them.
Many thanks in advance,
Matt