470,855 Members | 1,163 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

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 4851
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Dave | last post: by
2 posts views Thread by Brian Pelton | last post: by
reply views Thread by Iron Moped | last post: by
7 posts views Thread by Andrew Robinson | last post: by
35 posts views Thread by Lee Crabtree | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.