I have a class that all attributes have to be serialized; some of them are
read-only.
For example,

public int abc
{
get { return _abc; }
}

This attribute is a read-only but it will not be serialized. How can i
serizlize a read-only attributes ?

Re: Serialize a read-only attribute by Matt

Matt
Tue Jan 25 09:43:33 CST 2005

Hello mrVithan" mrvithan at hotmail.com,

Which serialization mechanism are you using? IXmlSerializable or ISerializable?

--
Matt Berther
http://www.mattberther.com

> I have a class that all attributes have to be serialized; some of them
> are
> read-only.
> For example,
> public int abc
> {
> get { return _abc; }
> }
> This attribute is a read-only but it will not be serialized. How can i
> serizlize a read-only attributes ?
>



Re: Serialize a read-only attribute by mrvithan

mrvithan
Tue Jan 25 09:53:05 CST 2005

[XmlElement ("abc")]
private int abc;

public int abc
{
get { return _abc; }
}

XmlSerializer s = new XmlSerializer (_classType);
TextWriter w = new StreamWriter (_filename);
s.Serialize( w, _object );
w.Close();

Here is my mechansim to seralize a object of a class.

"Matt Berther" wrote:

> Hello mrVithan" mrvithan at hotmail.com,
>
> Which serialization mechanism are you using? IXmlSerializable or ISerializable?
>
> --
> Matt Berther
> http://www.mattberther.com
>
> > I have a class that all attributes have to be serialized; some of them
> > are
> > read-only.
> > For example,
> > public int abc
> > {
> > get { return _abc; }
> > }
> > This attribute is a read-only but it will not be serialized. How can i
> > serizlize a read-only attributes ?
> >
>
>
>

Re: Serialize a read-only attribute by mrvithan

mrvithan
Wed Jan 26 12:53:01 CST 2005

Thx Matt ... but i found something more difficult.

I have a object (have a read-only attributes) that inherites from a base
object (no read-only attribute so i use XmlSerializer). Both object value
need to be serialized.
But i found that after i use the interface, my base object doesn't got
serialized automatically as using the XmlSerializer.

I think it should have way to ask the base object got serialized, doesn't it
? if no, i think i need to serialize all attributes (both inherited and base
class) in the System.Xml.Serialization.IXmlSerializable.WriteXml manually.
T_T

"Matt Berther" wrote:

> Hello mrVithan" mrvithan at hotmail.com,
>
> In this case, you will need to implement the IXmlSerializable interface,
> rather than using the XmlSerializer attributes. Keep in mind that by exposing
> the private variable you may be breaking the rules of encapsulation.
>
> Given that, you can look at http://dotnetified.com/PermaLink.aspx?guid=E86B447E-AA95-49B6-909F-CDA36ACF481F
> for a great example on how to implement IXmlSerializable.
>
> --
> Matt Berther
> http://www.mattberther.com
>
> > [XmlElement ("abc")]
> > private int abc;
> > public int abc
> > {
> > get { return _abc; }
> > }
> > XmlSerializer s = new XmlSerializer (_classType);
> > TextWriter w = new StreamWriter (_filename);
> > s.Serialize( w, _object );
> > w.Close();
> > Here is my mechansim to seralize a object of a class.
> >
> > "Matt Berther" wrote:
> >
> >> Hello mrVithan" mrvithan at hotmail.com,
> >>
> >> Which serialization mechanism are you using? IXmlSerializable or
> >> ISerializable?
> >>
> >> --
> >> Matt Berther
> >> http://www.mattberther.com
> >>> I have a class that all attributes have to be serialized; some of
> >>> them
> >>> are
> >>> read-only.
> >>> For example,
> >>> public int abc
> >>> {
> >>> get { return _abc; }
> >>> }
> >>> This attribute is a read-only but it will not be serialized. How can
> >>> i
> >>> serizlize a read-only attributes ?
>
>
>

Re: Serialize a read-only attribute by mrvithan

mrvithan
Wed Jan 26 12:59:04 CST 2005

Oh and also .... i also got this situation.

public class A : IXmlSerializable
{ }

public class B : IXmlSerializable
{
private A c;
}

How can i ask the class A got seralized when i am serializing class B ?

"Matt Berther" wrote:

> Hello mrVithan" mrvithan at hotmail.com,
>
> In this case, you will need to implement the IXmlSerializable interface,
> rather than using the XmlSerializer attributes. Keep in mind that by exposing
> the private variable you may be breaking the rules of encapsulation.
>
> Given that, you can look at http://dotnetified.com/PermaLink.aspx?guid=E86B447E-AA95-49B6-909F-CDA36ACF481F
> for a great example on how to implement IXmlSerializable.
>
> --
> Matt Berther
> http://www.mattberther.com
>
> > [XmlElement ("abc")]
> > private int abc;
> > public int abc
> > {
> > get { return _abc; }
> > }
> > XmlSerializer s = new XmlSerializer (_classType);
> > TextWriter w = new StreamWriter (_filename);
> > s.Serialize( w, _object );
> > w.Close();
> > Here is my mechansim to seralize a object of a class.
> >
> > "Matt Berther" wrote:
> >
> >> Hello mrVithan" mrvithan at hotmail.com,
> >>
> >> Which serialization mechanism are you using? IXmlSerializable or
> >> ISerializable?
> >>
> >> --
> >> Matt Berther
> >> http://www.mattberther.com
> >>> I have a class that all attributes have to be serialized; some of
> >>> them
> >>> are
> >>> read-only.
> >>> For example,
> >>> public int abc
> >>> {
> >>> get { return _abc; }
> >>> }
> >>> This attribute is a read-only but it will not be serialized. How can
> >>> i
> >>> serizlize a read-only attributes ?
>
>
>