473,236 Members | 1,410 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,236 software developers and data experts.

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 4980
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
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
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
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
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
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
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
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
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
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
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
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.