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 2149
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: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |