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 1 2134
okay - i have figured out what the problem is, and it is nothing to do
with the IXmlSerializable interface.
basically, I was trying to use the XmlSerializer in a manner whereby I
would specify that the object I was passing it was any of types A, B, C
or D. it can't handle this. you must specify the type you are passing
it to serialize and then only ever pass it this type. this main type
can contain the other types as part of an object graph, but that's as
far as it goes.
why does it need to know the type you're passing it anyway? surely that
it is marked serializable is enough.. ma****@tiscali.co.uk wrote: 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Waz |
last post by:
I'm having some problems serializing a class that contains
a collection. Creating the TestClass below and then
calling its Serialize method throws the error:
There was an error reflecting...
|
by: Gregory Khrapunovich |
last post by:
Hi,
I have a class that contains user configuration settings
and I would like to save it on disk when program exits
and restore data when program starts. XmlSerializer seems
like a natural...
|
by: Thomas D. |
last post by:
Situation:
---------------------------
I have an 'export'-wrapper to my regular objects. For each regular object
there is also an export object. An export object derives from the regular
object...
|
by: Thomas D. |
last post by:
Hello all,
I'm using the IXmlSerializable interface for a project and encounter some
problems when testing my webservice in a client application. I know this
interface is undocumented and not...
|
by: Jayne |
last post by:
I'm building a Web Service with the CLR 2.0 that returns a DataTable but I
get the followng error:
System.InvalidOperationException: There was an error generating the XML
document. --->...
|
by: Peter Nofelt |
last post by:
Hey All,
I am having issue with serializing a class and its base class using
..net 2.0. I am using IXmlSerializable
I've provided code displaying my at the bottom of this post. Thanks
ahead...
|
by: she_prog |
last post by:
I have a class derived from UserControl.
I need to serialize an object of this class, but only some properties
of it, as not all properties are serializable (some of the properties
coming from...
|
by: ilitirit |
last post by:
Can anyone explain why the following program doesn't work? The
attributes and elements of the MessageList class are not being
generated.
Am I doing something incorrectly? Or if this is a bug...
|
by: roynevo |
last post by:
Hi,
I'm trying to deserialize an object on the context of a remoting call
(RPC).
The deserialization seem to work fine, however when I'm casting the
result to the relevant class I get an...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| |