By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.