473,626 Members | 3,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serialize/DeSerialize Generics List

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("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>
Oct 17 '06 #1
2 35666
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>
Oct 17 '06 #2
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
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("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>
Oct 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
14294
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
5
24696
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream s); public void Deserialize(Stream s); Within the MyUserControl class, there is a field of type MyInnerClass
1
7083
by: Mike Pollett | last post by:
Hi, I have used the ISerializable interface before and the code below worked fine. Until I derived it from CollectionBase. The code will still serialize and deserialize the properties in this class and properties derived from this class but will not serialize or deserialize the properties in CollectionBase. Like InnerList, which is a read only property of CollectionBase. How can I serialize and deserialize the InnerList property of...
8
6861
by: Frank Rizzo | last post by:
How do I serialize Font object into a string that I can store in either INI file or the registry (I know it’s a bad thing, but need to do it nevertheless). Then, also, how do I deserialize it from that string back into a font object? Thanks.
0
2062
by: John Manion via .NET 247 | last post by:
Long Post, thanks for your patience... I have and XML file that looks something like this: <?xml version="1.0" encoding="utf-8" ?> <Settings> <Location> <X>30</X> <Y>40</Y> </Location> <Size>
17
1860
by: Peter | last post by:
How would would you deserialize this example below? Imports System Imports System.Collections Imports System.IO Imports System.Xml.Serialization Public Class App1 Shared Sub Main()
3
15170
by: Mirek Endys | last post by:
What is the best way to serialize object, that contains data in interfaces. Simple. I have instance of my object, that contains data in interfaces. What is the best way to XMLSerialize and deserialize this object? Thanks.
1
3662
by: davebaranas | last post by:
I am able to serialize this but I get a null exception when I try to deserialize it back Even if I don't make any child classes it throws "Object reference not set to an instance of an object." I must be missing something here Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click
1
1761
by: Tony Johansson | last post by:
Hello! Assume I have a class called Product which is defined with the attribute I create a collection by using the generic class List in this way List<Productproducts = new List<Product>(); product.Add(new Product(1, "some name 1",120)); product.Add(new Product(1, "some name 2",130)); product.Add(new Product(1, "some name 3",140));
0
8269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8203
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8642
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4206
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2630
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1515
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.