By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,136 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,136 IT Pros & Developers. It's quick & easy.

Serialize/DeSerialize Generics List

P: n/a
Joe
Hi

I have a Generics List in a PropertyGrid

I am able to Serialize it to XML but when I try to deserialize back to the
class of the PropertyGrid
The Constructor doesn't seem to fire to reload the saved settings

Can anyone see something that I have missed ?

Class of values
----------------
[XmlRoot("ValueString")]

public class ValueString

{

private string upper = "uppervalue";

private string lower = "lowervalue";

public ValueString()

{

}

public ValueString(string sUpper, string sLower)

{

upper = sUpper

lower = sLower;

}

[XmlElement("Upper")]

public string Upper

{

get { return upper; }

set { upper = value; }

}

[XmlElement("Lower")]

public string Lower

{

get { return lower; }

set { lower = value; }

}

}

PropertGrid Class

----------------

private List<ValueStringmyValues = new List<ValueString>();

myValues.Add("upper1","lower1");

myValues.Add("upper2","lower2");

[Category("Misc")]

[Browsable(false)]

[XmlArrayItem("ValArray")]

public List<ValueStringValuesStr

{

get

{

return myValues

}

set

{

myValues = value;
}

}

xml serialized from Class

-----

<ValuesStr>
<ValArray>
<Upper>upper1</Upper>
<Lower>lower1</Lower>
</ValArray>
<ValArray>
<Upper>upper2</Upper>
<Lower>lower2</Lower>
</ValArray>
</ValuesStr>
Oct 17 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
AFAIK, the way that the deserialize operation of XML Arrays works is
that it constructs an array object (not a List<>) from scratch and
assigns it - meanwhile deserialization requires only an IEnumerable.
Your List<works fine for deserializing since it's an IEnumerable, bu
not the other way because it's not an Array.

I might be wrong about this - but that's just he behaviour I'm
observing. Perhaps providing a public Array-based property that is
converted to-and-from your hidden List property (converting from lists
to arrays and vice versa is pretty simple) - then just serialize your
Array.

Joe wrote:
Hi

I have a Generics List in a PropertyGrid

I am able to Serialize it to XML but when I try to deserialize back to the
class of the PropertyGrid
The Constructor doesn't seem to fire to reload the saved settings

Can anyone see something that I have missed ?

Class of values
----------------
[XmlRoot("ValueString")]

public class ValueString

{

private string upper = "uppervalue";

private string lower = "lowervalue";

public ValueString()

{

}

public ValueString(string sUpper, string sLower)

{

upper = sUpper

lower = sLower;

}

[XmlElement("Upper")]

public string Upper

{

get { return upper; }

set { upper = value; }

}

[XmlElement("Lower")]

public string Lower

{

get { return lower; }

set { lower = value; }

}

}

PropertGrid Class

----------------

private List<ValueStringmyValues = new List<ValueString>();

myValues.Add("upper1","lower1");

myValues.Add("upper2","lower2");

[Category("Misc")]

[Browsable(false)]

[XmlArrayItem("ValArray")]

public List<ValueStringValuesStr

{

get

{

return myValues

}

set

{

myValues = value;
}

}

xml serialized from Class

-----

<ValuesStr>
<ValArray>
<Upper>upper1</Upper>
<Lower>lower1</Lower>
</ValArray>
<ValArray>
<Upper>upper2</Upper>
<Lower>lower2</Lower>
</ValArray>
</ValuesStr>
Oct 17 '06 #2

P: n/a
Ick, a bit of a brain fart there: I said "Deserialize" when I meant
"Serialize" a bunch of times. Serialize from IEnumerable, deserialize
from Array.

from
http://msdn2.microsoft.com/en-us/lib....xmlarray.aspx

"""Gets or sets an object that specifies how the XmlSerializer
serializes a public field or read/write property that returns an
*array*. """ (emph. mine).
Martin Z wrote:
AFAIK, the way that the deserialize operation of XML Arrays works is
that it constructs an array object (not a List<>) from scratch and
assigns it - meanwhile deserialization requires only an IEnumerable.
Your List<works fine for deserializing since it's an IEnumerable, bu
not the other way because it's not an Array.

I might be wrong about this - but that's just he behaviour I'm
observing. Perhaps providing a public Array-based property that is
converted to-and-from your hidden List property (converting from lists
to arrays and vice versa is pretty simple) - then just serialize your
Array.

Joe wrote:
Hi

I have a Generics List in a PropertyGrid

I am able to Serialize it to XML but when I try to deserialize back to the
class of the PropertyGrid
The Constructor doesn't seem to fire to reload the saved settings

Can anyone see something that I have missed ?

Class of values
----------------
[XmlRoot("ValueString")]

public class ValueString

{

private string upper = "uppervalue";

private string lower = "lowervalue";

public ValueString()

{

}

public ValueString(string sUpper, string sLower)

{

upper = sUpper

lower = sLower;

}

[XmlElement("Upper")]

public string Upper

{

get { return upper; }

set { upper = value; }

}

[XmlElement("Lower")]

public string Lower

{

get { return lower; }

set { lower = value; }

}

}

PropertGrid Class

----------------

private List<ValueStringmyValues = new List<ValueString>();

myValues.Add("upper1","lower1");

myValues.Add("upper2","lower2");

[Category("Misc")]

[Browsable(false)]

[XmlArrayItem("ValArray")]

public List<ValueStringValuesStr

{

get

{

return myValues

}

set

{

myValues = value;
}

}

xml serialized from Class

-----

<ValuesStr>
<ValArray>
<Upper>upper1</Upper>
<Lower>lower1</Lower>
</ValArray>
<ValArray>
<Upper>upper2</Upper>
<Lower>lower2</Lower>
</ValArray>
</ValuesStr>
Oct 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.