473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

HELP Problem serializing a structure

Hello,

I am having a problem trying to serialize this simple structure, I use it
in a collection class that derives from CollectionBase but I noticed that at
least for now the serialization exception ocurrs when i try to serialize
this structure (see below). The idea is that it would be serialized to:

< Param type="..." method="Get" required="true">something</Param>

I use the following to serialize:

WizarParameter wpar = new WizardParameter(typeof(int),
WizardParameterMethod.Get, true, "something");
StringWriter sw = new StringWriter();
XmlSerializer ser = new XmlSerializer(typeof(WizardParameter));
ser.Serialize(sw, wpar);

[Serializable]
[XmlRoot("Param")]
public struct WizardParameter

{

/// The system type contained by this parameter
[XmlAttribute("type")]
public System.Type Type;

/// The method that should be used to pass this paramete
[XmlAttribute("method")]
public WizardParameterMethod Method; /// enumeration

/// Whether the parameter is required or optional

[XmlAttribute("required")]
public bool IsRequired;

[XmlElement("key")]
public string Key;

public WizardParameter(Type type, WizardParameterMethod method, bool
required, string key)

{

this.Type = type;

this.Method = method;

this.IsRequired = required;

this.Key = key;

}

}
Nov 17 '05 #1
3 10788
Ed,

What are the details of the exception? I believe that you can't do this
properly because the XmlSerializer will only access public properties, and
can not access indexers either (which is how you would get the items in the
collection).

You might want to use the SoapFormatter (depending on how readable you
want the content to be).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"~~~ .NET Ed ~~~" <ti*********@abolishspam.now> wrote in message
news:eO**************@TK2MSFTNGP15.phx.gbl...
Hello,

I am having a problem trying to serialize this simple structure, I use
it in a collection class that derives from CollectionBase but I noticed
that at least for now the serialization exception ocurrs when i try to
serialize this structure (see below). The idea is that it would be
serialized to:

< Param type="..." method="Get" required="true">something</Param>

I use the following to serialize:

WizarParameter wpar = new WizardParameter(typeof(int),
WizardParameterMethod.Get, true, "something");
StringWriter sw = new StringWriter();
XmlSerializer ser = new XmlSerializer(typeof(WizardParameter));
ser.Serialize(sw, wpar);

[Serializable]
[XmlRoot("Param")]
public struct WizardParameter

{

/// The system type contained by this parameter
[XmlAttribute("type")]
public System.Type Type;

/// The method that should be used to pass this paramete
[XmlAttribute("method")]
public WizardParameterMethod Method; /// enumeration

/// Whether the parameter is required or optional

[XmlAttribute("required")]
public bool IsRequired;

[XmlElement("key")]
public string Key;

public WizardParameter(Type type, WizardParameterMethod method, bool
required, string key)

{

this.Type = type;

this.Method = method;

this.IsRequired = required;

this.Key = key;

}

}

Nov 17 '05 #2
Well I guess I will worry about the collection serialization when the time
comes. For now my problem is that this simple structure with four public
members (of which three should be serialized as attribute and the other as
inner text) and no indexers throws an InvalidOperationException with "There
was an error reflecting type WizardParameter" message.

Any ideas? I don't see why such a simple structure would not serialize. This
structure in particular should serialize as given below (a single element
with 3 attributes and inner text).

Emil

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
Ed,

What are the details of the exception? I believe that you can't do
this properly because the XmlSerializer will only access public
properties, and can not access indexers either (which is how you would get
the items in the collection).

You might want to use the SoapFormatter (depending on how readable you
want the content to be).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"~~~ .NET Ed ~~~" <ti*********@abolishspam.now> wrote in message
news:eO**************@TK2MSFTNGP15.phx.gbl...
Hello,

I am having a problem trying to serialize this simple structure, I use
it in a collection class that derives from CollectionBase but I noticed
that at least for now the serialization exception ocurrs when i try to
serialize this structure (see below). The idea is that it would be
serialized to:

< Param type="..." method="Get"
required="true">something</Param>

I use the following to serialize:

WizarParameter wpar = new WizardParameter(typeof(int),
WizardParameterMethod.Get, true, "something");
StringWriter sw = new StringWriter();
XmlSerializer ser = new XmlSerializer(typeof(WizardParameter));
ser.Serialize(sw, wpar);

[Serializable]
[XmlRoot("Param")]
public struct WizardParameter

{

/// The system type contained by this parameter
[XmlAttribute("type")]
public System.Type Type;

/// The method that should be used to pass this paramete
[XmlAttribute("method")]
public WizardParameterMethod Method; /// enumeration

/// Whether the parameter is required or optional

[XmlAttribute("required")]
public bool IsRequired;

[XmlElement("key")]
public string Key;

public WizardParameter(Type type, WizardParameterMethod method, bool
required, string key)

{

this.Type = type;

this.Method = method;

this.IsRequired = required;

this.Key = key;

}

}


Nov 17 '05 #3
I finally found out, the message given was not enough, but exploring the
actual exception object generated told me the culprit was the Type property.
For some reason it is not able to serialize a member of type System.Type.
Since I don't have more time to spend on it I worked around it by converting
the member to a string and simply using x.GetType().ToString() now both the
structure (items) and the custom collection (based on CollectionBase)
serialize properly and with the element/attribute names I chose.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
Ed,

What are the details of the exception? I believe that you can't do
this properly because the XmlSerializer will only access public
properties, and can not access indexers either (which is how you would get
the items in the collection).

You might want to use the SoapFormatter (depending on how readable you
want the content to be).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"~~~ .NET Ed ~~~" <ti*********@abolishspam.now> wrote in message
news:eO**************@TK2MSFTNGP15.phx.gbl...
Hello,

I am having a problem trying to serialize this simple structure, I use
it in a collection class that derives from CollectionBase but I noticed
that at least for now the serialization exception ocurrs when i try to
serialize this structure (see below). The idea is that it would be
serialized to:

< Param type="..." method="Get"
required="true">something</Param>

I use the following to serialize:

WizarParameter wpar = new WizardParameter(typeof(int),
WizardParameterMethod.Get, true, "something");
StringWriter sw = new StringWriter();
XmlSerializer ser = new XmlSerializer(typeof(WizardParameter));
ser.Serialize(sw, wpar);

[Serializable]
[XmlRoot("Param")]
public struct WizardParameter

{

/// The system type contained by this parameter
[XmlAttribute("type")]
public System.Type Type;

/// The method that should be used to pass this paramete
[XmlAttribute("method")]
public WizardParameterMethod Method; /// enumeration

/// Whether the parameter is required or optional

[XmlAttribute("required")]
public bool IsRequired;

[XmlElement("key")]
public string Key;

public WizardParameter(Type type, WizardParameterMethod method, bool
required, string key)

{

this.Type = type;

this.Method = method;

this.IsRequired = required;

this.Key = key;

}

}


Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: rgerla | last post by:
I'm trying to serialize a compount structure set for a web service, the sturctures are something like this: Public Structure UserPhone Public phonenumber As String Public phonetype As Char End...
0
by: rgerla | last post by:
I'm trying to serialize a compount structure set for a web service, the sturctures are something like this: Public Structure UserPhone Public phonenumber As String Public phonetype As Char End...
10
by: copx | last post by:
I want to save a struct to disk.... as plain text. At the moment I do it with a function that just writes the data using fprintf. I mean like this: fprintf(fp, "%d %d", my_struct.a, my_struct.b)...
5
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e....
2
by: Tobias Zimmergren | last post by:
Hi, just wondering what serializing really is, and howto use it? Thanks. Tobias __________________________________________________________________ Tobias ICQ#: 55986339 Current ICQ status: +...
0
by: olsonchris | last post by:
Hello all, I have what appears to be a simple question but after quite a bit of research I can't seem to find an answer. Basically, I am serializing a class into an XML document. This is...
6
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
2
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hi, I know this will sound like a lot of hand waving, and I'll be glad to supply some sample code if necessary, but I thought something obvious might jump out at someone without doing so. ...
12
by: Cagdas Ozgenc | last post by:
Greetings, When directly serializing C++ structures to a file with the standard library functions giving the address of the data and length of structure using the sizeof operator, do I risk...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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
0
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...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.