Serialize a read-only attribute

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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 ?
 
Hello mrVithan" mrvithan at hotmail.com,

Which serialization mechanism are you using? IXmlSerializable or ISerializable?
 
[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.
 
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 said:
Hello mrVithan" mrvithan at hotmail.com,

Which serialization mechanism are you using? IXmlSerializable or
ISerializable?
 
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 said:
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 said:
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 ?
 
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 said:
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 said:
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 ?
 
Hello mrVithan" mrvithan at hotmail.com,

public class B : IXmlSerializable
{
private A c;

public void ReadXml(XmlReader reader) // or whatever the signature is
{
XmlSerializer ser = new XmlSerializer(typeof(A));
c = (A)ser.Deserializer(reader);
}

public void WriteXml(XmlWriter writer) // or whatever the signature is
{
XmlSerializer ser = new XmlSerializer(typeof(A));
ser.Serialize(writer, c);
}
}

Keep in mind that if you do other things before the de/serialization, you'll
need to make sure the reader/writer is positioned correctly.

--
Matt Berther
http://www.mattberther.com
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 said:
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-CD
A36ACF481F 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.
:

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 ?
 
Hello mrVithan" mrvithan at hotmail.com,

This makes sense... When you serialize a class, you are serializing the entire
object graph. The serialization mechanism has no concept of an inheritance
heirarchy. You'll need to provide that manually.

--
Matt Berther
http://www.mattberther.com
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 said:
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-CD
A36ACF481F 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.
:

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 ?
 
Back
Top