Re: Serialize/DeSerialize Generics List
- From: "Martin Z" <martin.zarate@xxxxxxxxx>
- Date: 17 Oct 2006 10:42:19 -0700
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/library/system.xml.serialization.xmlattributes.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<ValueString> myValues = new List<ValueString>();
myValues.Add("upper1","lower1");
myValues.Add("upper2","lower2");
[Category("Misc")]
[Browsable(false)]
[XmlArrayItem("ValArray")]
public List<ValueString> ValuesStr
{
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>
.
- References:
- Serialize/DeSerialize Generics List
- From: Joe
- Re: Serialize/DeSerialize Generics List
- From: Martin Z
- Serialize/DeSerialize Generics List
- Prev by Date: Re: Disposing Objects?
- Next by Date: error message
- Previous by thread: Re: Serialize/DeSerialize Generics List
- Next by thread: error message
- Index(es):
Relevant Pages
|