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 IXmlSerializabl e to get round the fact that Exception is not
serializable.
Here's the implementation code from the (abstract) superclass:
#region IXmlSerializabl e Members
public void WriteXml(System .Xml.XmlWriter writer)
{
writer.WriteAtt ributeString(Xm lNamespaces.Xml SchemaInstance. Prefix,
"type", XmlNamespaces.X mlSchemaInstanc e.Uri.ToString( ),
this.GetType(). Name);
WriteAdditional ContentXml(writ er);
}
public System.Xml.Sche ma.XmlSchema GetSchema()
{
XmlSchema schema = new XmlSchema();
schema.Id = this.GetType(). FullName;
schema.TargetNa mespace = XmlNamespaces.S ervice.Uri.ToSt ring();
schema.Namespac es.Add(XmlNames paces.Service.P refix,
XmlNamespaces.S ervice.Uri.ToSt ring());
schema.Namespac es.Add(XmlNames paces.XmlSchema Instance.Prefix ,
XmlNamespaces.X mlSchemaInstanc e.Uri.ToString( ));
XmlSchemaComple xType state = new XmlSchemaComple xType();
state.Name = this.GetType(). Name;
schema.Items.Ad d(state);
XmlSchemaSequen ce sequence = new XmlSchemaSequen ce();
sequence.MinOcc urs = 0;
sequence.MaxOcc urs = 1;
AddSchemaConten t(schema, sequence);
if (sequence.Items .Count > 0)
{
state.Particle = sequence;
}
schema.Compile( new ValidationEvent Handler(schemaV alidationHandle r));
return schema;
}
public virtual void ReadXml(System. Xml.XmlReader reader)
{
}
protected virtual void WriteAdditional ContentXml(XmlW riter writer)
{
}
protected virtual void AddSchemaConten t(XmlSchema schema,
XmlSchemaSequen ce parentSequence)
{
}
private void schemaValidatio nHandler(object sender,
ValidationEvent Args args)
{
Trace.WriteLine (args.Message);
Debug.Fail(args .Message + ":" + Environment.New Line +
Environment.New Line + args.Exception. ToString() + Environment.New Line +
Environment.New Line);
}
#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 InvalidOperatio nException ("There was an error
generating the XML document"), the inner exception of which is an
InvalidCastExce ption ("Specified cast is not valid"), and the stack
trace of which is:
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o, XmlSerializerNa mespaces namespaces, String
encodingStyle)
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter
xmlWriter, Object o)
at
Limit.Company.L ossAdjustment.T ests.State.Stat esTest.serializ e(FeeState
state, XmlSerializer serializer) in
q:\projects\lim it\limit.compan y.lossadjustmen t.tests\state\s tatestest.cs:li ne
131
at
Limit.Company.L ossAdjustment.T ests.State.Stat esTest.XmlSeria lizeErrorState( )
in
q:\projects\lim it\limit.compan y.lossadjustmen t.tests\state\s tatestest.cs:li ne
71
--InvalidOperatio nException
at
Microsoft.Xml.S erialization.Ge neratedAssembly .XmlSerializati onWriter1.Write 1_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 2167
okay - i have figured out what the problem is, and it is nothing to do
with the IXmlSerializabl e 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 IXmlSerializabl e to get round the fact that Exception is
not serializable.
Here's the implementation code from the (abstract) superclass:
#region IXmlSerializabl e Members
public void WriteXml(System .Xml.XmlWriter writer) { writer.WriteAtt ributeString(Xm lNamespaces.Xml SchemaInstance. Prefix, "type", XmlNamespaces.X mlSchemaInstanc e.Uri.ToString( ), this.GetType(). Name); WriteAdditional ContentXml(writ er); }
public System.Xml.Sche ma.XmlSchema GetSchema() {
XmlSchema schema = new XmlSchema(); schema.Id = this.GetType(). FullName; schema.TargetNa mespace = XmlNamespaces.S ervice.Uri.ToSt ring();
schema.Namespac es.Add(XmlNames paces.Service.P refix, XmlNamespaces.S ervice.Uri.ToSt ring()); schema.Namespac es.Add(XmlNames paces.XmlSchema Instance.Prefix , XmlNamespaces.X mlSchemaInstanc e.Uri.ToString( ));
XmlSchemaComple xType state = new XmlSchemaComple xType(); state.Name = this.GetType(). Name; schema.Items.Ad d(state);
XmlSchemaSequen ce sequence = new XmlSchemaSequen ce(); sequence.MinOcc urs = 0; sequence.MaxOcc urs = 1;
AddSchemaConten t(schema, sequence);
if (sequence.Items .Count > 0) { state.Particle = sequence; }
schema.Compile( new ValidationEvent Handler(schemaV alidationHandle r));
return schema; }
public virtual void ReadXml(System. Xml.XmlReader reader) { }
protected virtual void WriteAdditional ContentXml(XmlW riter writer) { }
protected virtual void AddSchemaConten t(XmlSchema schema, XmlSchemaSequen ce parentSequence) { }
private void schemaValidatio nHandler(object sender, ValidationEvent Args args) { Trace.WriteLine (args.Message); Debug.Fail(args .Message + ":" + Environment.New Line + Environment.New Line + args.Exception. ToString() + Environment.New Line
+ Environment.New Line); }
#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 InvalidOperatio nException ("There was an error generating the XML document"), the inner exception of which is an InvalidCastExce ption ("Specified cast is not valid"), and the stack trace of which is:
at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter xmlWriter, Object o, XmlSerializerNa mespaces namespaces, String encodingStyle) at System.Xml.Seri alization.XmlSe rializer.Serial ize(XmlWriter xmlWriter, Object o) at
Limit.Company.L ossAdjustment.T ests.State.Stat esTest.serializ e(FeeState state, XmlSerializer serializer) in
q:\projects\lim it\limit.compan y.lossadjustmen t.tests\state\s tatestest.cs:li ne 131 at
Limit.Company.L ossAdjustment.T ests.State.Stat esTest.XmlSeria lizeErrorState( ) in
q:\projects\lim it\limit.compan y.lossadjustmen t.tests\state\s tatestest.cs:li ne 71 --InvalidOperatio nException at
Microsoft.Xml.S erialization.Ge neratedAssembly .XmlSerializati onWriter1.Write 1_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 'TestApp.TestClass'
If I use place an over the TheDeliveryList
property it works fine. Another option may be to implement
the IXmlSerializable interface but I was hoping that this
would not be necessary.
|
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 choice. And it works fine. There is only
one problem. All members of the class must be declared
public in order to be serialized. It breaks the
encapsulation and can potentially be a problem (some
members should only be set through functions because...
|
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 and has for each property in the base object an extra boolean
property. That extra properties tells wether to include the base property in
the final export (= webservice) or not. That is because not all the
properties may be exposed all the time...
|
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 intended for use, but I think this is the
only solution for my situation. I searched the web, in the hope finding the
answer, without any luck, so my final hope is with you.
Let me explain the situation:
I have an 'export'-wrapper to my regular...
|
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. ---> System.InvalidOperationException: Cannot serialize the
DataTable. DataTable name is not set.
at System.Data.DataTable.WriteXmlSchema(XmlWriter writer, Boolean
writeHierarchy)
at...
| |
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 of time for any help or feedback.
Cheers,
Peter
|
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 UserControl are like that). When serializing, how
could I ignore all the properties coming from the UserControl
class? I know there is XmlIgnoreAttribute, but how could I set it
to every property of UserControl, as it is not my class?
Thank you...
|
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 in .NET, is
there a known workaround?
using System;
|
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 InvalidCastException.
Note:
* the remoting server has TypeFilterLevel to Full to remove security
restrictions.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |