Serialize/DeSerialize Generics List

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe

    Serialize/DeSerialize Generics List

    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("ValueS tring")]

    public class ValueString

    {

    private string upper = "uppervalue ";

    private string lower = "lowervalue ";

    public ValueString()

    {

    }

    public ValueString(str ing sUpper, string sLower)

    {

    upper = sUpper

    lower = sLower;

    }

    [XmlElement("Upp er")]

    public string Upper

    {

    get { return upper; }

    set { upper = value; }

    }

    [XmlElement("Low er")]

    public string Lower

    {

    get { return lower; }

    set { lower = value; }

    }

    }



    PropertGrid Class

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

    private List<ValueStrin gmyValues = new List<ValueStrin g>();

    myValues.Add("u pper1","lower1" );

    myValues.Add("u pper2","lower2" );



    [Category("Misc" )]

    [Browsable(false )]

    [XmlArrayItem("V alArray")]

    public List<ValueStrin gValuesStr

    {

    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>


  • Martin Z

    #2
    Re: Serialize/DeSerialize Generics List

    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("ValueS tring")]
    >
    public class ValueString
    >
    {
    >
    private string upper = "uppervalue ";
    >
    private string lower = "lowervalue ";
    >
    public ValueString()
    >
    {
    >
    }
    >
    public ValueString(str ing sUpper, string sLower)
    >
    {
    >
    upper = sUpper
    >
    lower = sLower;
    >
    }
    >
    [XmlElement("Upp er")]
    >
    public string Upper
    >
    {
    >
    get { return upper; }
    >
    set { upper = value; }
    >
    }
    >
    [XmlElement("Low er")]
    >
    public string Lower
    >
    {
    >
    get { return lower; }
    >
    set { lower = value; }
    >
    }
    >
    }
    >
    >
    >
    PropertGrid Class
    >
    ----------------
    >
    private List<ValueStrin gmyValues = new List<ValueStrin g>();
    >
    myValues.Add("u pper1","lower1" );
    >
    myValues.Add("u pper2","lower2" );
    >
    >
    >
    [Category("Misc" )]
    >
    [Browsable(false )]
    >
    [XmlArrayItem("V alArray")]
    >
    public List<ValueStrin gValuesStr
    >
    {
    >
    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>

    Comment

    • Martin Z

      #3
      Re: Serialize/DeSerialize Generics List

      Ick, a bit of a brain fart there: I said "Deserializ e" when I meant
      "Serialize" a bunch of times. Serialize from IEnumerable, deserialize
      from Array.

      from
      Gets or sets an object that specifies how the XmlSerializer serializes a public field or read/write property that returns an array.


      """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("ValueS tring")]

      public class ValueString

      {

      private string upper = "uppervalue ";

      private string lower = "lowervalue ";

      public ValueString()

      {

      }

      public ValueString(str ing sUpper, string sLower)

      {

      upper = sUpper

      lower = sLower;

      }

      [XmlElement("Upp er")]

      public string Upper

      {

      get { return upper; }

      set { upper = value; }

      }

      [XmlElement("Low er")]

      public string Lower

      {

      get { return lower; }

      set { lower = value; }

      }

      }



      PropertGrid Class

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

      private List<ValueStrin gmyValues = new List<ValueStrin g>();

      myValues.Add("u pper1","lower1" );

      myValues.Add("u pper2","lower2" );



      [Category("Misc" )]

      [Browsable(false )]

      [XmlArrayItem("V alArray")]

      public List<ValueStrin gValuesStr

      {

      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>

      Comment

      Working...