Home All Groups Group Topic Archive Search About

HELP Problem serializing a structure

Author
4 Apr 2005 6:46 PM
~~~ .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;

}

}

Author
4 Apr 2005 7:16 PM
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.


--
               - Nicholas Paldino [.NET/C# MVP]
               - mvp@spam.guard.caspershouse.com

Show quoteHide quote
"~~~ .NET Ed ~~~" <tiredofspam@abolishspam.now> wrote in message
news:eO39QaUOFHA.3512@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;
>
> }
>
> }
>
>
Are all your drivers up to date? click for free checkup

Author
4 Apr 2005 8:32 PM
~~~ .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

Show quoteHide quote
"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in
message news:%23qFzErUOFHA.1500@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]
>               - mvp@spam.guard.caspershouse.com
>
> "~~~ .NET Ed ~~~" <tiredofspam@abolishspam.now> wrote in message
> news:eO39QaUOFHA.3512@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;
>>
>> }
>>
>> }
>>
>>
>
>
Author
4 Apr 2005 11:02 PM
~~~ .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.

Show quoteHide quote
"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in
message news:%23qFzErUOFHA.1500@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]
>               - mvp@spam.guard.caspershouse.com
>
> "~~~ .NET Ed ~~~" <tiredofspam@abolishspam.now> wrote in message
> news:eO39QaUOFHA.3512@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;
>>
>> }
>>
>> }
>>
>>
>
>

Bookmark and Share