HELP Problem serializing a structure

  • Thread starter ~~~ .NET Ed ~~~
  • Start date
N

~~~ .NET Ed ~~~

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;

}

}
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
N

~~~ .NET Ed ~~~

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 said:
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]
- (e-mail address removed)

~~~ .NET Ed ~~~ said:
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;

}

}
 
N

~~~ .NET Ed ~~~

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 said:
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]
- (e-mail address removed)

~~~ .NET Ed ~~~ said:
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;

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top