473,508 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlSerializer would not serialize properties in the class derived from List<T>

I have following 3 classes

public class MyMainClass
{
MyCollection<MyObject> m_oMyObjectCollection = null;

private string m_sID = string.Empty;

public MyCollection<MyObject> Collection
{
get
{
return this.m_oMyObjectCollection;
}
set
{
this.m_oMyObjectCollection = value;
}
}

[XmlAttribute]
public string ID
{
get
{
return this.m_sID;
}
set
{
this.m_sID = value;
}
}
}

public class MyCollection<T>:System.Collections.Generic.List<T>
{
private string m_sMyString=string.Empty;

[XmlAttribute]
public string @Default
{
get
{
return this.m_sMyString;
}
set
{
this.m_sMyString = value;
}
}
}

public class MyObject
{
string m_sID = string.Empty;

[XmlAttribute]
public string ID
{
get
{
return this.m_sID;
}
set
{
this.m_sID = value;
}
}
}

when I use XmlSerializer to serialize the object. the Default property
in the MyCollection class would not get serialized. The serialize code
I used as following:

MyCollection<MyObject> oCollection=new MyCollection<MyObject>();
oCollection.Default = "DEF001";
MyObject oMyObject = new MyObject();
oMyObject.ID = "ID001";
oCollection.Add(oMyObject);
MyMainClass oMain = new MyMainClass();
oMain.Collection = oCollection;
oMain.ID = "MAIN01";
XmlSerializer xs = new XmlSerializer(typeof(MyMainClass));
StringWriter sw = new StringWriter();
xs.Serialize(sw, oMain);
Debug.WriteLine(sw.ToString());
Could anyone please point out what I did wrong?

thanks

Jinsong
May 10 '06 #1
2 4994
I have found that the reason that my "Default" property isn't
serialized is because XmlSerializer.Serialize does not serialize
custome property in a custom collection class by "design". Is there
any work around?

On Wed, 10 May 2006 16:45:24 -0400, Jinsong Liu
<Ji*****@mssolution.com.nospam> wrote:
I have following 3 classes

public class MyMainClass
{
MyCollection<MyObject> m_oMyObjectCollection = null;

private string m_sID = string.Empty;

public MyCollection<MyObject> Collection
{
get
{
return this.m_oMyObjectCollection;
}
set
{
this.m_oMyObjectCollection = value;
}
}

[XmlAttribute]
public string ID
{
get
{
return this.m_sID;
}
set
{
this.m_sID = value;
}
}
}

public class MyCollection<T>:System.Collections.Generic.List<T>
{
private string m_sMyString=string.Empty;

[XmlAttribute]
public string @Default
{
get
{
return this.m_sMyString;
}
set
{
this.m_sMyString = value;
}
}
}

public class MyObject
{
string m_sID = string.Empty;

[XmlAttribute]
public string ID
{
get
{
return this.m_sID;
}
set
{
this.m_sID = value;
}
}
}

when I use XmlSerializer to serialize the object. the Default property
in the MyCollection class would not get serialized. The serialize code
I used as following:

MyCollection<MyObject> oCollection=new MyCollection<MyObject>();
oCollection.Default = "DEF001";
MyObject oMyObject = new MyObject();
oMyObject.ID = "ID001";
oCollection.Add(oMyObject);
MyMainClass oMain = new MyMainClass();
oMain.Collection = oCollection;
oMain.ID = "MAIN01";
XmlSerializer xs = new XmlSerializer(typeof(MyMainClass));
StringWriter sw = new StringWriter();
xs.Serialize(sw, oMain);
Debug.WriteLine(sw.ToString());
Could anyone please point out what I did wrong?

thanks

Jinsong


May 10 '06 #2
Since you are using .Net 2.0, you can use the now officially supported
IXmlSerializable interface so that you can control how the object is
serialized. To control the serialization, implement the ReadXml and
WriteXml methods to control the XmlReader and XmlWriter classes used to read
and write the XML.

Don Demsak
[XML MVP]
www.donxml.com

"Jinsong Liu" wrote:
I have found that the reason that my "Default" property isn't
serialized is because XmlSerializer.Serialize does not serialize
custome property in a custom collection class by "design". Is there
any work around?

On Wed, 10 May 2006 16:45:24 -0400, Jinsong Liu
<Ji*****@mssolution.com.nospam> wrote:
I have following 3 classes

public class MyMainClass
{
MyCollection<MyObject> m_oMyObjectCollection = null;

private string m_sID = string.Empty;

public MyCollection<MyObject> Collection
{
get
{
return this.m_oMyObjectCollection;
}
set
{
this.m_oMyObjectCollection = value;
}
}

[XmlAttribute]
public string ID
{
get
{
return this.m_sID;
}
set
{
this.m_sID = value;
}
}
}

public class MyCollection<T>:System.Collections.Generic.List<T>
{
private string m_sMyString=string.Empty;

[XmlAttribute]
public string @Default
{
get
{
return this.m_sMyString;
}
set
{
this.m_sMyString = value;
}
}
}

public class MyObject
{
string m_sID = string.Empty;

[XmlAttribute]
public string ID
{
get
{
return this.m_sID;
}
set
{
this.m_sID = value;
}
}
}

when I use XmlSerializer to serialize the object. the Default property
in the MyCollection class would not get serialized. The serialize code
I used as following:

MyCollection<MyObject> oCollection=new MyCollection<MyObject>();
oCollection.Default = "DEF001";
MyObject oMyObject = new MyObject();
oMyObject.ID = "ID001";
oCollection.Add(oMyObject);
MyMainClass oMain = new MyMainClass();
oMain.Collection = oCollection;
oMain.ID = "MAIN01";
XmlSerializer xs = new XmlSerializer(typeof(MyMainClass));
StringWriter sw = new StringWriter();
xs.Serialize(sw, oMain);
Debug.WriteLine(sw.ToString());
Could anyone please point out what I did wrong?

thanks

Jinsong


May 16 '06 #3

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

Similar topics

14
5589
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
4
52921
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
2
2666
by: Brian Pelton | last post by:
I am not sure how to fix this problem I've stumbled into... I have a list<> of an interface type. I need to pass that list to a method that adds more objects to the list. But, eventually, I...
0
1733
by: Iron Moped | last post by:
I'm airing frustration here, but why does LinkedList<not support the same sort and search methods as List<>? I want a container that does not support random access, allows forward and reverse...
7
57519
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
56
5092
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be...
45
18764
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
0
1334
by: SC | last post by:
How do I create at runtime a list of string (List<stringstrs = new List<string>(); fill it) and then bind it to a DataGridViewColumnBox? Setting the columns DataSource to the list doesn't...
4
10605
by: Peted | last post by:
I have the following code public enum pdfFlags { PFD_DRAW_TO_WINDOW, PFD_DRAW_TO_BITMAP, PFD_SUPPORT_GDI, PFD_SUPPORT_OPENGL, PFD_GENERIC_ACCELERATED, PFD_GENERIC_FORMAT,
35
5833
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
0
7401
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...
1
7063
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...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5059
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...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3211
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...
0
1568
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 ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
432
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...

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.