Re: Serialize/DeSerialize Generics List



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>

.



Relevant Pages

  • Re: Serialize/DeSerialize Generics List
    ... not the other way because it's not an Array. ... converted to-and-from your hidden List property (converting from lists ... I am able to Serialize it to XML but when I try to deserialize back to the ...
    (microsoft.public.dotnet.languages.csharp)
  • Overridden XML serialization is deserializing incorrectly.
    ... So I'm using XmlSerializer to serialize out a wrapper object that ... an array of types to be included in the serialization. ... MethodToTest.Result deserialize as objects and I could tell what they ... Here is the class definition for the MethodToTest object: ...
    (microsoft.public.dotnet.languages.csharp)
  • Serialization Performance
    ... In my test sample the array that I'm serializing ... On my system this array takes ~55 seconds to serialize and deserialize while ...
    (microsoft.public.dotnet.languages.vb)
  • RE: XML Deserialization (IXmlSerializable implementation)
    ... Could you show me how you serialize and deserialize the array? ... to make a repro on my computer so that I can make a better troubleshooting. ...
    (microsoft.public.dotnet.xml)
  • Re: Serialization
    ... But the array I defined is inside a structure. ... Define the second class which inherits from the class on line 1. ... Serialize an instance of the second class,no error occurs. ... Deserialize the file from line 4. ...
    (microsoft.public.dotnet.languages.vb)