Xml Serialization of a custom class

E

Eric Workman

If I have a class that holds two string values, so for instance.

Public Class myClass{

private string s1;
private string s2;

public myClass(str1, str2){
s1 = str1;
s2 = str2;
}
}

And then another class which I wish to serialize using an
XmlSerializer that holds an instance variable of type myClass, so for
instance.

Public Class mySerializable{

public string str1;
public string str2;
public myClass mc;

public mySerializable(s1, s2, myClass){
str1 = s1;
str2 = s2;
mc = myClass;
}
}

When I Serilize this using an XML Serializer, the values come out fine
for str1 and str2, but for mc it is empty.

My Question is...Is there a way to specify in myClass somehow that
when it is serialized by an XmlSerializer to simply output s1 as its
value? If not, is there any work around to make this work?

Thanks,

Eric Workman
 
M

Mickey Williams

XML serialization only serializes public fields and properties (and
therefore requires less trust). If you want full-fidelity, you'll need to
use runtime serialization.
 
E

Eric Workman

Really what I need is the objects to be serialized into xml for use in
an XmlDocument object, is there any way to do that using Runtime
Serialization?

Thanks Again,

Eric Workman
 
M

Mickey Williams

If you can deal with the overhead, you can use the soap formatter. Otherwise
you'll need to write an XML formatter for runtime serialization (or expose
the fields/properties).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top